1
0

rtgui_system.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * File : rtgui_system.c
  3. * This file is part of RTGUI in RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-04 Bernard first version
  13. */
  14. #include <rtgui/rtgui.h>
  15. #include <rtgui/driver.h>
  16. #include <rtgui/image.h>
  17. #include <rtgui/rtgui_theme.h>
  18. #include <rtgui/rtgui_system.h>
  19. #include <rtgui/rtgui_server.h>
  20. #include <rtgui/widgets/window.h>
  21. // #define RTGUI_EVENT_DEBUG
  22. #ifdef __WIN32__
  23. #define RTGUI_EVENT_DEBUG
  24. #define RTGUI_MEM_TRACE
  25. #endif
  26. void rtgui_system_server_init()
  27. {
  28. /* init image */
  29. rtgui_system_image_init();
  30. /* init font */
  31. rtgui_font_system_init();
  32. /* init rtgui server */
  33. rtgui_panel_init();
  34. rtgui_topwin_init();
  35. rtgui_server_init();
  36. /* init theme */
  37. rtgui_system_theme_init();
  38. }
  39. /************************************************************************/
  40. /* RTGUI Thread Wrapper */
  41. /************************************************************************/
  42. #ifdef RTGUI_EVENT_DEBUG
  43. const char *event_string[] =
  44. {
  45. /* panel event */
  46. "PANEL_ATTACH", /* attach to a panel */
  47. "PANEL_DETACH", /* detach from a panel */
  48. "PANEL_SHOW", /* show in a panel */
  49. "PANEL_HIDE", /* hide from a panel */
  50. "PANEL_INFO", /* panel information */
  51. "PANEL_RESIZE", /* resize panel */
  52. "PANEL_FULLSCREEN", /* to full screen */
  53. "PANEL_NORMAL", /* to normal screen */
  54. /* window event */
  55. "WIN_CREATE", /* create a window */
  56. "WIN_DESTROY", /* destroy a window */
  57. "WIN_SHOW", /* show a window */
  58. "WIN_HIDE", /* hide a window */
  59. "WIN_ACTIVATE", /* activate a window */
  60. "WIN_DEACTIVATE", /* deactivate a window */
  61. "WIN_CLOSE", /* close a window */
  62. "WIN_MOVE", /* move a window */
  63. "WIN_RESIZE", /* resize a window */
  64. "SET_WM", /* set window manager */
  65. "UPDATE_BEGIN", /* begin of update rect */
  66. "UPDATE_END", /* end of update rect */
  67. "MONITOR_ADD", /* add a monitor rect */
  68. "MONITOR_REMOVE", /* remove a monitor rect*/
  69. "PAINT", /* paint on screen */
  70. "TIMER", /* timer */
  71. /* clip rect information */
  72. "CLIP_INFO", /* clip rect info */
  73. /* mouse and keyboard event */
  74. "MOUSE_MOTION", /* mouse motion */
  75. "MOUSE_BUTTON", /* mouse button info */
  76. "KBD", /* keyboard info */
  77. /* user command event */
  78. "COMMAND", /* user command */
  79. /* request's status event */
  80. "STATUS", /* request result */
  81. "SCROLLED", /* scroll bar scrolled */
  82. "RESIZE", /* widget resize */
  83. };
  84. #define DBG_MSG(x) rt_kprintf x
  85. static void rtgui_event_dump(rt_thread_t tid, rtgui_event_t* event)
  86. {
  87. char* sender = "(unknown)";
  88. if (event->sender != RT_NULL) sender = event->sender->name;
  89. if ((event->type == RTGUI_EVENT_TIMER) ||
  90. (event->type == RTGUI_EVENT_UPDATE_BEGIN) ||
  91. (event->type == RTGUI_EVENT_UPDATE_END))
  92. {
  93. /* don't dump timer event */
  94. return ;
  95. }
  96. rt_kprintf("%s -- %s --> %s ", sender, event_string[event->type], tid->name);
  97. switch (event->type)
  98. {
  99. case RTGUI_EVENT_PAINT:
  100. {
  101. struct rtgui_event_paint *paint = (struct rtgui_event_paint *)event;
  102. if(paint->wid != RT_NULL)
  103. rt_kprintf("win: %s", paint->wid->title);
  104. }
  105. break;
  106. case RTGUI_EVENT_KBD:
  107. {
  108. struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd*) event;
  109. if (ekbd->wid != RT_NULL)
  110. rt_kprintf("win: %s", ekbd->wid->title);
  111. if (RTGUI_KBD_IS_UP(ekbd)) rt_kprintf(", up");
  112. else rt_kprintf(", down");
  113. }
  114. break;
  115. case RTGUI_EVENT_CLIP_INFO:
  116. {
  117. struct rtgui_event_clip_info *info = (struct rtgui_event_clip_info *)event;
  118. if(info->wid != RT_NULL)
  119. rt_kprintf("win: %s", info->wid->title);
  120. #ifdef RTGUI_USING_SMALL_SIZE
  121. rt_kprintf(" clip no. %d", info->num_rect);
  122. #else
  123. {
  124. rtgui_rect_t *rect;
  125. rt_uint32_t index;
  126. rect = (rtgui_rect_t*)(info + 1);
  127. for (index = 0; index < info->num_rect; index ++)
  128. {
  129. rt_kprintf(" (x1:%d, y1:%d, x2:%d, y2:%d)", rect->x1, rect->y1, rect->x2, rect->y2);
  130. rect ++;
  131. }
  132. }
  133. #endif
  134. }
  135. break;
  136. case RTGUI_EVENT_WIN_CREATE:
  137. {
  138. struct rtgui_event_win_create *create = (struct rtgui_event_win_create*)event;
  139. rt_kprintf(" win: %s at (x1:%d, y1:%d, x2:%d, y2:%d)",
  140. #ifdef RTGUI_USING_SMALL_SIZE
  141. create->wid->title,
  142. RTGUI_WIDGET(create->wid)->extent.x1,
  143. RTGUI_WIDGET(create->wid)->extent.y1,
  144. RTGUI_WIDGET(create->wid)->extent.x2,
  145. RTGUI_WIDGET(create->wid)->extent.y2);
  146. #else
  147. create->title,
  148. create->extent.x1,
  149. create->extent.y1,
  150. create->extent.x2,
  151. create->extent.y2);
  152. #endif
  153. }
  154. break;
  155. case RTGUI_EVENT_UPDATE_END:
  156. {
  157. struct rtgui_event_update_end* update_end = (struct rtgui_event_update_end*)event;
  158. rt_kprintf("(x:%d, y1:%d, x2:%d, y2:%d)", update_end->rect.x1,
  159. update_end->rect.y1,
  160. update_end->rect.x2,
  161. update_end->rect.y2);
  162. }
  163. break;
  164. case RTGUI_EVENT_WIN_ACTIVATE:
  165. case RTGUI_EVENT_WIN_DEACTIVATE:
  166. case RTGUI_EVENT_WIN_SHOW:
  167. {
  168. struct rtgui_event_win *win = (struct rtgui_event_win *)event;
  169. if(win->wid != RT_NULL)
  170. rt_kprintf("win: %s", win->wid->title);
  171. }
  172. break;
  173. case RTGUI_EVENT_WIN_MOVE:
  174. {
  175. struct rtgui_event_win_move *win = (struct rtgui_event_win_move *)event;
  176. if(win->wid != RT_NULL)
  177. {
  178. rt_kprintf("win: %s", win->wid->title);
  179. rt_kprintf(" to (x:%d, y:%d)", win->x, win->y);
  180. }
  181. }
  182. break;
  183. case RTGUI_EVENT_WIN_RESIZE:
  184. {
  185. struct rtgui_event_win_resize* win = (struct rtgui_event_win_resize *)event;
  186. if (win->wid != RT_NULL)
  187. {
  188. rt_kprintf("win: %s, rect(x1:%d, y1:%d, x2:%d, y2:%d)", win->wid->title,
  189. RTGUI_WIDGET(win->wid)->extent.x1,
  190. RTGUI_WIDGET(win->wid)->extent.y1,
  191. RTGUI_WIDGET(win->wid)->extent.x2,
  192. RTGUI_WIDGET(win->wid)->extent.y2);
  193. }
  194. }
  195. break;
  196. case RTGUI_EVENT_MOUSE_BUTTON:
  197. case RTGUI_EVENT_MOUSE_MOTION:
  198. {
  199. struct rtgui_event_mouse *mouse = (struct rtgui_event_mouse*)event;
  200. if (mouse->button & RTGUI_MOUSE_BUTTON_LEFT) rt_kprintf("left ");
  201. else rt_kprintf("right ");
  202. if (mouse->button & RTGUI_MOUSE_BUTTON_DOWN) rt_kprintf("down ");
  203. else rt_kprintf("up ");
  204. if (mouse->wid != RT_NULL)
  205. rt_kprintf("win: %s at (%d, %d)", mouse->wid->title,
  206. mouse->x, mouse->y);
  207. else
  208. rt_kprintf("(%d, %d)", mouse->x, mouse->y);
  209. }
  210. break;
  211. case RTGUI_EVENT_MONITOR_ADD:
  212. {
  213. struct rtgui_event_monitor *monitor = (struct rtgui_event_monitor*)event;
  214. if (monitor->panel != RT_NULL)
  215. {
  216. #if 0
  217. rt_kprintf("panel: %s, the rect is:(%d, %d) - (%d, %d)", monitor->panel->name,
  218. monitor->rect.x1, monitor->rect.y1,
  219. monitor->rect.x2, monitor->rect.y2);
  220. #endif
  221. rt_kprintf("the rect is:(%d, %d) - (%d, %d)",
  222. monitor->rect.x1, monitor->rect.y1,
  223. monitor->rect.x2, monitor->rect.y2);
  224. }
  225. else if (monitor->wid != RT_NULL)
  226. {
  227. rt_kprintf("win: %s, the rect is:(%d, %d) - (%d, %d)", monitor->wid->title,
  228. monitor->rect.x1, monitor->rect.y1,
  229. monitor->rect.x2, monitor->rect.y2);
  230. }
  231. }
  232. break;
  233. }
  234. rt_kprintf("\n");
  235. }
  236. #else
  237. #define DBG_MSG(x)
  238. #define rtgui_event_dump(tid, event)
  239. #endif
  240. rtgui_thread_t* rtgui_thread_register(rt_thread_t tid, rt_mq_t mq)
  241. {
  242. rtgui_thread_t* thread = rtgui_malloc(sizeof(struct rtgui_thread));
  243. if (thread != RT_NULL)
  244. {
  245. DBG_MSG(("register a rtgui thread: %s, tid: 0x%p\n", tid->name, tid));
  246. /* set tid and mq */
  247. thread->tid = tid;
  248. thread->mq = mq;
  249. thread->widget = RT_NULL;
  250. thread->on_idle = RT_NULL;
  251. /* set user thread */
  252. tid->user_data = (rt_uint32_t)thread;
  253. }
  254. return thread;
  255. }
  256. void rtgui_thread_deregister(rt_thread_t tid)
  257. {
  258. struct rtgui_thread* thread;
  259. /* find rtgui_thread */
  260. thread = (struct rtgui_thread*) (tid->user_data);
  261. if (thread != RT_NULL)
  262. {
  263. /* remove rtgui_thread */
  264. tid->user_data = 0;
  265. /* free rtgui_thread */
  266. rtgui_free(thread);
  267. }
  268. }
  269. /* get current gui thread */
  270. rtgui_thread_t* rtgui_thread_self()
  271. {
  272. struct rtgui_thread* thread;
  273. rt_thread_t self;
  274. /* get current thread */
  275. self = rt_thread_self();
  276. thread = (struct rtgui_thread*)(self->user_data);
  277. return thread;
  278. }
  279. void rtgui_thread_set_onidle(rtgui_idle_func onidle)
  280. {
  281. struct rtgui_thread* thread;
  282. thread = rtgui_thread_self();
  283. RT_ASSERT(thread != RT_NULL);
  284. thread->on_idle = onidle;
  285. }
  286. rtgui_idle_func rtgui_thread_get_onidle()
  287. {
  288. struct rtgui_thread* thread;
  289. thread = rtgui_thread_self();
  290. RT_ASSERT(thread != RT_NULL);
  291. return thread->on_idle;
  292. }
  293. extern rt_thread_t rt_thread_find(char* name);
  294. rt_thread_t rtgui_thread_get_server()
  295. {
  296. return rt_thread_find("rtgui");
  297. }
  298. void rtgui_thread_set_widget(struct rtgui_widget* widget)
  299. {
  300. struct rtgui_thread* thread;
  301. /* get rtgui_thread */
  302. thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
  303. if (thread != RT_NULL) thread->widget = widget;
  304. }
  305. struct rtgui_widget* rtgui_thread_get_widget()
  306. {
  307. struct rtgui_thread* thread;
  308. /* get rtgui_thread */
  309. thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
  310. return thread == RT_NULL? RT_NULL : thread->widget;
  311. }
  312. rt_err_t rtgui_thread_send(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  313. {
  314. rt_err_t result;
  315. struct rtgui_thread* thread;
  316. rtgui_event_dump(tid, event);
  317. /* if (event->type != RTGUI_EVENT_TIMER)
  318. rt_kprintf("event size: %d\n", event_size); */
  319. /* find rtgui_thread */
  320. thread = (struct rtgui_thread*) (tid->user_data);
  321. if (thread == RT_NULL) return -RT_ERROR;
  322. result = rt_mq_send(thread->mq, event, event_size);
  323. if (result != RT_EOK)
  324. {
  325. if (event->type != RTGUI_EVENT_TIMER)
  326. rt_kprintf("send event to %s failed\n", thread->tid->name);
  327. }
  328. return result;
  329. }
  330. rt_err_t rtgui_thread_send_urgent(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  331. {
  332. rt_err_t result;
  333. struct rtgui_thread* thread;
  334. rtgui_event_dump(tid, event);
  335. /* find rtgui_thread */
  336. thread = (struct rtgui_thread*) (tid->user_data);
  337. if (thread == RT_NULL) return -RT_ERROR;
  338. result = rt_mq_urgent(thread->mq, event, event_size);
  339. if (result != RT_EOK)
  340. rt_kprintf("send ergent event failed\n");
  341. return result;
  342. }
  343. rt_err_t rtgui_thread_send_sync(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  344. {
  345. rt_err_t r;
  346. struct rtgui_thread* thread;
  347. rt_int32_t ack_buffer, ack_status;
  348. struct rt_mailbox ack_mb;
  349. rtgui_event_dump(tid, event);
  350. /* init ack mailbox */
  351. r = rt_mb_init(&ack_mb, "ack", &ack_buffer, 1, 0);
  352. if ( r!= RT_EOK) goto __return;
  353. /* find rtgui_thread */
  354. thread = (struct rtgui_thread*) (tid->user_data);
  355. if (thread == RT_NULL){ r = RT_ERROR; goto __return; }
  356. event->ack = &ack_mb;
  357. r = rt_mq_send(thread->mq, event, event_size);
  358. if (r != RT_EOK)
  359. {
  360. rt_kprintf("send sync event failed\n");
  361. goto __return;
  362. }
  363. r = rt_mb_recv(&ack_mb, (rt_uint32_t*)&ack_status, RT_WAITING_FOREVER);
  364. if ( r!= RT_EOK) goto __return;
  365. if (ack_status != RTGUI_STATUS_OK) r = -RT_ERROR;
  366. else r = RT_EOK;
  367. /* fini ack mailbox */
  368. rt_mb_detach(&ack_mb);
  369. __return:
  370. return r;
  371. }
  372. rt_err_t rtgui_thread_ack(rtgui_event_t* event, rt_int32_t status)
  373. {
  374. if (event != RT_NULL &&
  375. event->ack != RT_NULL)
  376. {
  377. rt_mb_send(event->ack, status);
  378. }
  379. return RT_EOK;
  380. }
  381. rt_err_t rtgui_thread_recv(rtgui_event_t* event, rt_size_t event_size)
  382. {
  383. struct rtgui_thread* thread;
  384. rt_err_t r;
  385. /* find rtgui_thread */
  386. thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
  387. if (thread == RT_NULL) return -RT_ERROR;
  388. r = rt_mq_recv(thread->mq, event, event_size, RT_WAITING_FOREVER);
  389. return r;
  390. }
  391. rt_err_t rtgui_thread_recv_nosuspend(rtgui_event_t* event, rt_size_t event_size)
  392. {
  393. struct rtgui_thread* thread;
  394. rt_err_t r;
  395. /* find rtgui_thread */
  396. thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
  397. if (thread == RT_NULL) return -RT_ERROR;
  398. r = rt_mq_recv(thread->mq, event, event_size, 0);
  399. return r;
  400. }
  401. rt_err_t rtgui_thread_recv_filter(rt_uint32_t type, rtgui_event_t* event, rt_size_t event_size)
  402. {
  403. struct rtgui_thread* thread;
  404. /* find rtgui_thread */
  405. thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
  406. if (thread == RT_NULL) return -RT_ERROR;
  407. while (rt_mq_recv(thread->mq, event, event_size, RT_WAITING_FOREVER) == RT_EOK)
  408. {
  409. if (event->type == type)
  410. {
  411. return RT_EOK;
  412. }
  413. else
  414. {
  415. /* let widget to handle event */
  416. if (thread->widget != RT_NULL &&
  417. thread->widget->event_handler != RT_NULL)
  418. {
  419. thread->widget->event_handler(thread->widget, event);
  420. }
  421. }
  422. }
  423. return -RT_ERROR;
  424. }
  425. /************************************************************************/
  426. /* RTGUI Timer */
  427. /************************************************************************/
  428. static void rtgui_time_out(void* parameter)
  429. {
  430. rtgui_timer_t* timer;
  431. struct rtgui_event_timer event;
  432. timer = (rtgui_timer_t*)parameter;
  433. /*
  434. * Note: event_timer can not use RTGUI_EVENT_TIMER_INIT to init, for there is no
  435. * thread context
  436. */
  437. event.parent.type = RTGUI_EVENT_TIMER;
  438. event.parent.sender = RT_NULL;
  439. event.timer = timer;
  440. rtgui_thread_send(timer->tid, &(event.parent), sizeof(struct rtgui_event_timer));
  441. }
  442. rtgui_timer_t* rtgui_timer_create(rt_int32_t time, rt_base_t flag, rtgui_timeout_func timeout, void* parameter)
  443. {
  444. rtgui_timer_t* timer;
  445. timer = (rtgui_timer_t*) rtgui_malloc(sizeof(struct rtgui_timer));
  446. timer->tid = rt_thread_self();
  447. timer->timeout = timeout;
  448. timer->user_data = parameter;
  449. /* init rt-thread timer */
  450. rt_timer_init(&(timer->timer), "rtgui", rtgui_time_out, timer, time, (rt_uint8_t)flag);
  451. return timer;
  452. }
  453. void rtgui_timer_destory(rtgui_timer_t* timer)
  454. {
  455. RT_ASSERT(timer != RT_NULL);
  456. /* stop timer firstly */
  457. rtgui_timer_stop(timer);
  458. /* detach rt-thread timer */
  459. rt_timer_detach(&(timer->timer));
  460. rtgui_free(timer);
  461. }
  462. void rtgui_timer_start(rtgui_timer_t* timer)
  463. {
  464. RT_ASSERT(timer != RT_NULL);
  465. /* start rt-thread timer */
  466. rt_timer_start(&(timer->timer));
  467. }
  468. void rtgui_timer_stop (rtgui_timer_t* timer)
  469. {
  470. RT_ASSERT(timer != RT_NULL);
  471. /* stop rt-thread timer */
  472. rt_timer_stop(&(timer->timer));
  473. }
  474. /************************************************************************/
  475. /* RTGUI Memory Management */
  476. /************************************************************************/
  477. #ifdef RTGUI_MEM_TRACE
  478. struct rtgui_mem_info
  479. {
  480. rt_uint32_t allocated_size;
  481. rt_uint32_t max_allocated;
  482. };
  483. struct rtgui_mem_info mem_info;
  484. #define MEMTRACE_MAX 4096
  485. #define MEMTRACE_HASH_SIZE 256
  486. struct rti_memtrace_item
  487. {
  488. void* mb_ptr; /* memory block pointer */
  489. rt_uint32_t mb_len; /* memory block length */
  490. struct rti_memtrace_item* next;
  491. };
  492. struct rti_memtrace_item trace_list[MEMTRACE_MAX];
  493. struct rti_memtrace_item *item_hash[MEMTRACE_HASH_SIZE];
  494. struct rti_memtrace_item *item_free;
  495. rt_bool_t rti_memtrace_inited = 0;
  496. void rti_memtrace_init()
  497. {
  498. struct rti_memtrace_item *item;
  499. rt_uint32_t index;
  500. rt_memset(trace_list, 0, sizeof(trace_list));
  501. rt_memset(item_hash, 0, sizeof(item_hash));
  502. item_free = &trace_list[0];
  503. item = &trace_list[0];
  504. for (index = 1; index < MEMTRACE_HASH_SIZE; index ++)
  505. {
  506. item->next = &trace_list[index];
  507. item = item->next;
  508. }
  509. item->next = RT_NULL;
  510. }
  511. void rti_malloc_hook(void* ptr, rt_uint32_t len)
  512. {
  513. rt_uint32_t index;
  514. struct rti_memtrace_item* item;
  515. if (item_free == RT_NULL) return;
  516. mem_info.allocated_size += len;
  517. if (mem_info.max_allocated < mem_info.allocated_size)
  518. mem_info.max_allocated = mem_info.allocated_size;
  519. /* lock context */
  520. item = item_free;
  521. item_free = item->next;
  522. item->mb_ptr = ptr;
  523. item->mb_len = len;
  524. item->next = RT_NULL;
  525. /* get hash item index */
  526. index = ((rt_uint32_t)ptr) % MEMTRACE_HASH_SIZE;
  527. if (item_hash[index] != RT_NULL)
  528. {
  529. /* add to list */
  530. item->next = item_hash[index];
  531. item_hash[index] = item;
  532. }
  533. else
  534. {
  535. /* set list header */
  536. item_hash[index] = item;
  537. }
  538. /* unlock context */
  539. }
  540. void rti_free_hook(void* ptr)
  541. {
  542. rt_uint32_t index;
  543. struct rti_memtrace_item *item;
  544. /* get hash item index */
  545. index = ((rt_uint32_t)ptr) % MEMTRACE_HASH_SIZE;
  546. if (item_hash[index] != RT_NULL)
  547. {
  548. item = item_hash[index];
  549. if (item->mb_ptr == ptr)
  550. {
  551. /* delete item from list */
  552. item_hash[index] = item->next;
  553. }
  554. else
  555. {
  556. /* find ptr in list */
  557. while (item->next != RT_NULL && item->next->mb_ptr != ptr)
  558. item = item->next;
  559. /* delete item from list */
  560. if (item->next != RT_NULL)
  561. {
  562. struct rti_memtrace_item* i;
  563. i = item->next;
  564. item->next = item->next->next;
  565. item = i;
  566. }
  567. else
  568. {
  569. /* not found */
  570. return;
  571. }
  572. }
  573. /* reduce allocated size */
  574. mem_info.allocated_size -= item->mb_len;
  575. /* clear item */
  576. rt_memset(item, 0, sizeof(struct rti_memtrace_item));
  577. /* add item to the free list */
  578. item->next = item_free;
  579. item_free = item;
  580. }
  581. }
  582. #endif
  583. void* rtgui_malloc(rt_size_t size)
  584. {
  585. void* ptr;
  586. ptr = rt_malloc(size);
  587. #ifdef RTGUI_MEM_TRACE
  588. if (rti_memtrace_inited == 0)
  589. {
  590. rti_memtrace_init();
  591. rti_memtrace_inited = 1;
  592. }
  593. if (ptr != RT_NULL)
  594. rti_malloc_hook(ptr, size);
  595. #endif
  596. return ptr;
  597. }
  598. void* rtgui_realloc(void* ptr, rt_size_t size)
  599. {
  600. void* new_ptr;
  601. #ifdef RTGUI_MEM_TRACE
  602. new_ptr = rtgui_malloc(size);
  603. if ((new_ptr != RT_NULL) && (ptr != RT_NULL))
  604. {
  605. rt_memcpy(new_ptr, ptr, size);
  606. rtgui_free(ptr);
  607. }
  608. #else
  609. new_ptr = rt_realloc(ptr, size);
  610. #endif
  611. return new_ptr;
  612. }
  613. void rtgui_free(void* ptr)
  614. {
  615. #ifdef RTGUI_MEM_TRACE
  616. if (ptr != RT_NULL)
  617. rti_free_hook(ptr);
  618. #endif
  619. rt_free(ptr);
  620. }