rtgui_system.c 17 KB

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