rtgui_system.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. rt_kprintf("event size: %d\n", event_size);
  336. /* find rtgui_thread */
  337. thread = (struct rtgui_thread*) (tid->user_data);
  338. if (thread == RT_NULL) return -RT_ERROR;
  339. result = rt_mq_urgent(thread->mq, event, event_size);
  340. if (result != RT_EOK)
  341. rt_kprintf("send ergent event failed\n");
  342. return result;
  343. }
  344. rt_err_t rtgui_thread_send_sync(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  345. {
  346. rt_err_t r;
  347. struct rtgui_thread* thread;
  348. rt_int32_t ack_buffer, ack_status;
  349. struct rt_mailbox ack_mb;
  350. rtgui_event_dump(tid, event);
  351. rt_kprintf("event size: %d\n", event_size);
  352. /* init ack mailbox */
  353. r = rt_mb_init(&ack_mb, "ack", &ack_buffer, 1, 0);
  354. if ( r!= RT_EOK) goto __return;
  355. /* find rtgui_thread */
  356. thread = (struct rtgui_thread*) (tid->user_data);
  357. if (thread == RT_NULL){ r = RT_ERROR; goto __return; }
  358. event->ack = &ack_mb;
  359. r = rt_mq_send(thread->mq, event, event_size);
  360. if (r != RT_EOK)
  361. {
  362. rt_kprintf("send sync event failed\n");
  363. goto __return;
  364. }
  365. r = rt_mb_recv(&ack_mb, (rt_uint32_t*)&ack_status, RT_WAITING_FOREVER);
  366. if ( r!= RT_EOK) goto __return;
  367. if (ack_status != RTGUI_STATUS_OK) r = -RT_ERROR;
  368. else r = RT_EOK;
  369. /* fini ack mailbox */
  370. rt_mb_detach(&ack_mb);
  371. __return:
  372. return r;
  373. }
  374. rt_err_t rtgui_thread_ack(rtgui_event_t* event, rt_int32_t status)
  375. {
  376. if (event != RT_NULL &&
  377. event->ack != RT_NULL)
  378. {
  379. rt_mb_send(event->ack, status);
  380. }
  381. return RT_EOK;
  382. }
  383. rt_err_t rtgui_thread_recv(rtgui_event_t* event, rt_size_t event_size)
  384. {
  385. struct rtgui_thread* thread;
  386. rt_err_t r;
  387. /* find rtgui_thread */
  388. thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
  389. if (thread == RT_NULL) return -RT_ERROR;
  390. r = rt_mq_recv(thread->mq, event, event_size, RT_WAITING_FOREVER);
  391. return r;
  392. }
  393. rt_err_t rtgui_thread_recv_nosuspend(rtgui_event_t* event, rt_size_t event_size)
  394. {
  395. struct rtgui_thread* thread;
  396. rt_err_t r;
  397. /* find rtgui_thread */
  398. thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
  399. if (thread == RT_NULL) return -RT_ERROR;
  400. r = rt_mq_recv(thread->mq, event, event_size, 0);
  401. return r;
  402. }
  403. rt_err_t rtgui_thread_recv_filter(rt_uint32_t type, rtgui_event_t* event, rt_size_t event_size)
  404. {
  405. struct rtgui_thread* thread;
  406. /* find rtgui_thread */
  407. thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
  408. if (thread == RT_NULL) return -RT_ERROR;
  409. while (rt_mq_recv(thread->mq, event, event_size, RT_WAITING_FOREVER) == RT_EOK)
  410. {
  411. if (event->type == type)
  412. {
  413. return RT_EOK;
  414. }
  415. else
  416. {
  417. /* let widget to handle event */
  418. if (thread->widget != RT_NULL &&
  419. thread->widget->event_handler != RT_NULL)
  420. {
  421. thread->widget->event_handler(thread->widget, event);
  422. }
  423. }
  424. }
  425. return -RT_ERROR;
  426. }
  427. /************************************************************************/
  428. /* RTGUI Timer */
  429. /************************************************************************/
  430. static void rtgui_time_out(void* parameter)
  431. {
  432. rtgui_timer_t* timer;
  433. struct rtgui_event_timer event;
  434. timer = (rtgui_timer_t*)parameter;
  435. /*
  436. * Note: event_timer can not use RTGUI_EVENT_TIMER_INIT to init, for there is no
  437. * thread context
  438. */
  439. event.parent.type = RTGUI_EVENT_TIMER;
  440. event.parent.sender = RT_NULL;
  441. event.timer = timer;
  442. rtgui_thread_send(timer->tid, &(event.parent), sizeof(struct rtgui_event_timer));
  443. }
  444. rtgui_timer_t* rtgui_timer_create(rt_int32_t time, rt_base_t flag, rtgui_timeout_func timeout, void* parameter)
  445. {
  446. rtgui_timer_t* timer;
  447. timer = (rtgui_timer_t*) rtgui_malloc(sizeof(struct rtgui_timer));
  448. timer->tid = rt_thread_self();
  449. timer->timeout = timeout;
  450. timer->user_data = parameter;
  451. /* init rt-thread timer */
  452. rt_timer_init(&(timer->timer), "rtgui", rtgui_time_out, timer, time, (rt_uint8_t)flag);
  453. return timer;
  454. }
  455. void rtgui_timer_destory(rtgui_timer_t* timer)
  456. {
  457. RT_ASSERT(timer != RT_NULL);
  458. /* stop timer firstly */
  459. rtgui_timer_stop(timer);
  460. /* detach rt-thread timer */
  461. rt_timer_detach(&(timer->timer));
  462. rtgui_free(timer);
  463. }
  464. void rtgui_timer_start(rtgui_timer_t* timer)
  465. {
  466. RT_ASSERT(timer != RT_NULL);
  467. /* start rt-thread timer */
  468. rt_timer_start(&(timer->timer));
  469. }
  470. void rtgui_timer_stop (rtgui_timer_t* timer)
  471. {
  472. RT_ASSERT(timer != RT_NULL);
  473. /* stop rt-thread timer */
  474. rt_timer_stop(&(timer->timer));
  475. }
  476. /************************************************************************/
  477. /* RTGUI Memory Management */
  478. /************************************************************************/
  479. #ifdef RTGUI_MEM_TRACE
  480. struct rtgui_mem_info
  481. {
  482. rt_uint32_t allocated_size;
  483. rt_uint32_t max_allocated;
  484. };
  485. struct rtgui_mem_info mem_info;
  486. #define MEMTRACE_MAX 4096
  487. #define MEMTRACE_HASH_SIZE 256
  488. struct rti_memtrace_item
  489. {
  490. void* mb_ptr; /* memory block pointer */
  491. rt_uint32_t mb_len; /* memory block length */
  492. struct rti_memtrace_item* next;
  493. };
  494. struct rti_memtrace_item trace_list[MEMTRACE_MAX];
  495. struct rti_memtrace_item *item_hash[MEMTRACE_HASH_SIZE];
  496. struct rti_memtrace_item *item_free;
  497. rt_bool_t rti_memtrace_inited = 0;
  498. void rti_memtrace_init()
  499. {
  500. struct rti_memtrace_item *item;
  501. rt_uint32_t index;
  502. rt_memset(trace_list, 0, sizeof(trace_list));
  503. rt_memset(item_hash, 0, sizeof(item_hash));
  504. item_free = &trace_list[0];
  505. item = &trace_list[0];
  506. for (index = 1; index < MEMTRACE_HASH_SIZE; index ++)
  507. {
  508. item->next = &trace_list[index];
  509. item = item->next;
  510. }
  511. item->next = RT_NULL;
  512. }
  513. void rti_malloc_hook(void* ptr, rt_uint32_t len)
  514. {
  515. rt_uint32_t index;
  516. struct rti_memtrace_item* item;
  517. if (item_free == RT_NULL) return;
  518. mem_info.allocated_size += len;
  519. if (mem_info.max_allocated < mem_info.allocated_size)
  520. mem_info.max_allocated = mem_info.allocated_size;
  521. /* lock context */
  522. item = item_free;
  523. item_free = item->next;
  524. item->mb_ptr = ptr;
  525. item->mb_len = len;
  526. item->next = RT_NULL;
  527. /* get hash item index */
  528. index = ((rt_uint32_t)ptr) % MEMTRACE_HASH_SIZE;
  529. if (item_hash[index] != RT_NULL)
  530. {
  531. /* add to list */
  532. item->next = item_hash[index];
  533. item_hash[index] = item;
  534. }
  535. else
  536. {
  537. /* set list header */
  538. item_hash[index] = item;
  539. }
  540. /* unlock context */
  541. }
  542. void rti_free_hook(void* ptr)
  543. {
  544. rt_uint32_t index;
  545. struct rti_memtrace_item *item;
  546. /* get hash item index */
  547. index = ((rt_uint32_t)ptr) % MEMTRACE_HASH_SIZE;
  548. if (item_hash[index] != RT_NULL)
  549. {
  550. item = item_hash[index];
  551. if (item->mb_ptr == ptr)
  552. {
  553. /* delete item from list */
  554. item_hash[index] = item->next;
  555. }
  556. else
  557. {
  558. /* find ptr in list */
  559. while (item->next != RT_NULL && item->next->mb_ptr != ptr)
  560. item = item->next;
  561. /* delete item from list */
  562. if (item->next != RT_NULL)
  563. {
  564. struct rti_memtrace_item* i;
  565. i = item->next;
  566. item->next = item->next->next;
  567. item = i;
  568. }
  569. else
  570. {
  571. /* not found */
  572. return;
  573. }
  574. }
  575. /* reduce allocated size */
  576. mem_info.allocated_size -= item->mb_len;
  577. /* clear item */
  578. rt_memset(item, 0, sizeof(struct rti_memtrace_item));
  579. /* add item to the free list */
  580. item->next = item_free;
  581. item_free = item;
  582. }
  583. }
  584. #endif
  585. void* rtgui_malloc(rt_size_t size)
  586. {
  587. void* ptr;
  588. ptr = rt_malloc(size);
  589. #ifdef RTGUI_MEM_TRACE
  590. if (rti_memtrace_inited == 0)
  591. {
  592. rti_memtrace_init();
  593. rti_memtrace_inited = 1;
  594. }
  595. if (ptr != RT_NULL)
  596. rti_malloc_hook(ptr, size);
  597. #endif
  598. return ptr;
  599. }
  600. void rtgui_free(void* ptr)
  601. {
  602. #ifdef RTGUI_MEM_TRACE
  603. if (ptr != RT_NULL)
  604. rti_free_hook(ptr);
  605. #endif
  606. rt_free(ptr);
  607. }