rtgui_system.c 17 KB

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