memheap.c 19.9 KB
Newer Older
1 2 3 4 5
/*
 * File      : memheap.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2012, RT-Thread Development Team
 *
B
Bernard Xiong 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 20 21 22
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-04-10     Bernard      first implementation
23
 * 2012-10-16     Bernard      add the mutex lock for heap object.
24 25
 * 2012-12-29     Bernard      memheap can be used as system heap.
 *                             change mutex lock to semaphore lock.
B
Bernard Xiong 已提交
26
 * 2013-04-10     Bernard      add rt_memheap_realloc function.
B
Bernard Xiong 已提交
27
 * 2013-05-24     Bernard      fix the rt_memheap_realloc issue.
28
 * 2013-07-11     Grissiom     fix the memory block splitting issue.
29
 */
30

31
#include <rthw.h>
32 33 34 35 36
#include <rtthread.h>

#ifdef RT_USING_MEMHEAP

/* dynamic pool magic and mask */
37 38 39 40
#define RT_MEMHEAP_MAGIC        0x1ea01ea0
#define RT_MEMHEAP_MASK         0xfffffffe
#define RT_MEMHEAP_USED         0x01
#define RT_MEMHEAP_FREED        0x00
41

42 43
#define RT_MEMHEAP_IS_USED(i)   ((i)->magic & RT_MEMHEAP_USED)
#define RT_MEMHEAP_MINIALLOC    12
44

45 46
#define RT_MEMHEAP_SIZE         RT_ALIGN(sizeof(struct rt_memheap_item), RT_ALIGN_SIZE)
#define MEMITEM_SIZE(item)      ((rt_uint32_t)item->next - (rt_uint32_t)item - RT_MEMHEAP_SIZE)
47 48 49 50 51 52 53 54 55

/*
 * The initialized memory pool will be:
 * +-----------------------------------+--------------------------+
 * | whole freed memory block          | Used Memory Block Tailer |
 * +-----------------------------------+--------------------------+
 *
 * block_list --> whole freed memory block
 *
56 57
 * The length of Used Memory Block Tailer is 0,
 * which is prevents block merging across list
58
 */
59 60 61 62
rt_err_t rt_memheap_init(struct rt_memheap *memheap,
                         const char        *name,
                         void              *start_addr,
                         rt_uint32_t        size)
63
{
64
    struct rt_memheap_item *item;
65

66
    RT_ASSERT(memheap != RT_NULL);
67

68 69
    /* initialize pool object */
    rt_object_init(&(memheap->parent), RT_Object_Class_MemHeap, name);
70

71 72
    memheap->start_addr     = start_addr;
    memheap->pool_size      = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
73
    memheap->available_size = memheap->pool_size - (2 * RT_MEMHEAP_SIZE);
74
    memheap->max_used_size  = memheap->pool_size - memheap->available_size;
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    /* initialize the free list header */
    item            = &(memheap->free_header);
    item->magic     = RT_MEMHEAP_MAGIC;
    item->pool_ptr  = memheap;
    item->next      = RT_NULL;
    item->prev      = RT_NULL;
    item->next_free = item;
    item->prev_free = item;

    /* set the free list to free list header */
    memheap->free_list = item;

    /* initialize the first big memory block */
    item            = (struct rt_memheap_item *)start_addr;
    item->magic     = RT_MEMHEAP_MAGIC;
    item->pool_ptr  = memheap;
    item->next      = RT_NULL;
    item->prev      = RT_NULL;
    item->next_free = item;
    item->prev_free = item;

    item->next = (struct rt_memheap_item *)
        ((rt_uint8_t *)item + memheap->available_size + RT_MEMHEAP_SIZE);
    item->prev = item->next;

    /* block list header */
    memheap->block_list = item;

    /* place the big memory block to free list */
    item->next_free = memheap->free_list->next_free;
    item->prev_free = memheap->free_list;
    memheap->free_list->next_free->prev_free = item;
    memheap->free_list->next_free            = item;

    /* move to the end of memory pool to build a small tailer block,
     * which prevents block merging
     */
    item = item->next;
    /* it's a used memory block */
    item->magic     = RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED;
    item->pool_ptr  = memheap;
    item->next      = (struct rt_memheap_item *)start_addr;
    item->prev      = (struct rt_memheap_item *)start_addr;
    /* not in free list */
    item->next_free = item->prev_free = RT_NULL;

122 123
    /* initialize semaphore lock */
    rt_sem_init(&(memheap->lock), name, 1, RT_IPC_FLAG_FIFO);
124 125

    RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
126
                 ("memory heap: start addr 0x%08x, size %d, free list header 0x%08x\n",
127
                  start_addr, size, &(memheap->free_header)));
128

129
    return RT_EOK;
130
}
131
RTM_EXPORT(rt_memheap_init);
132

133
rt_err_t rt_memheap_detach(struct rt_memheap *heap)
134
{
135
    RT_ASSERT(heap);
136

137 138
    rt_object_detach(&(heap->lock.parent.parent));
    rt_object_detach(&(heap->parent));
139

140 141
    /* Return a successful completion. */
    return RT_EOK;
142
}
143
RTM_EXPORT(rt_memheap_detach);
144

145
void *rt_memheap_alloc(struct rt_memheap *heap, rt_uint32_t size)
146
{
147 148 149 150 151 152 153 154 155 156 157
    rt_err_t result;
    rt_uint32_t free_size;
    struct rt_memheap_item *header_ptr;

    RT_ASSERT(heap != RT_NULL);

    /* align allocated size */
    size = RT_ALIGN(size, RT_ALIGN_SIZE);
    if (size < RT_MEMHEAP_MINIALLOC)
        size = RT_MEMHEAP_MINIALLOC;

158 159
    RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate %d on heap:%8.*s",
                                    size, RT_NAME_MAX, heap->parent.name));
160 161 162 163 164 165 166

    if (size < heap->available_size)
    {
        /* search on free list */
        free_size = 0;

        /* lock memheap */
167
        result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
168 169 170 171 172 173 174 175 176 177 178 179
        if (result != RT_EOK)
        {
            rt_set_errno(result);

            return RT_NULL;
        }

        /* get the first free memory block */
        header_ptr = heap->free_list->next_free;
        while (header_ptr != heap->free_list && free_size < size)
        {
            /* get current freed memory block size */
B
Bernard Xiong 已提交
180
            free_size = MEMITEM_SIZE(header_ptr);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
            if (free_size < size)
            {
                /* move to next free memory block */
                header_ptr = header_ptr->next_free;
            }
        }

        /* determine if the memory is available. */
        if (free_size >= size)
        {
            /* a block that satisfies the request has been found. */

            /* determine if the block needs to be split. */
            if (free_size >= (size + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC))
            {
                struct rt_memheap_item *new_ptr;

                /* split the block. */
                new_ptr = (struct rt_memheap_item *)
                          (((rt_uint8_t *)header_ptr) + size + RT_MEMHEAP_SIZE);

                RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
203
                             ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
204 205 206 207
                              header_ptr,
                              header_ptr->next,
                              header_ptr->prev,
                              new_ptr));
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                /* mark the new block as a memory block and freed. */
                new_ptr->magic = RT_MEMHEAP_MAGIC;

                /* put the pool pointer into the new block. */
                new_ptr->pool_ptr = heap;

                /* break down the block list */
                new_ptr->prev          = header_ptr;
                new_ptr->next          = header_ptr->next;
                header_ptr->next->prev = new_ptr;
                header_ptr->next       = new_ptr;

                /* remove header ptr from free list */
                header_ptr->next_free->prev_free = header_ptr->prev_free;
                header_ptr->prev_free->next_free = header_ptr->next_free;
                header_ptr->next_free = RT_NULL;
                header_ptr->prev_free = RT_NULL;

                /* insert new_ptr to free list */
                new_ptr->next_free = heap->free_list->next_free;
                new_ptr->prev_free = heap->free_list;
                heap->free_list->next_free->prev_free = new_ptr;
                heap->free_list->next_free            = new_ptr;
232
                RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x\n",
233 234
                                                new_ptr->next_free,
                                                new_ptr->prev_free));
235

236 237 238 239
                /* decrement the available byte count.  */
                heap->available_size = heap->available_size -
                                       size -
                                       RT_MEMHEAP_SIZE;
240 241
                if (heap->pool_size - heap->available_size > heap->max_used_size)
                    heap->max_used_size = heap->pool_size - heap->available_size;
242 243 244 245 246
            }
            else
            {
                /* decrement the entire free size from the available bytes count. */
                heap->available_size = heap->available_size - free_size;
247 248
                if (heap->pool_size - heap->available_size > heap->max_used_size)
                    heap->max_used_size = heap->pool_size - heap->available_size;
249 250 251

                /* remove header_ptr from free list */
                RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
252
                             ("one block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x\n",
253 254 255
                              header_ptr,
                              header_ptr->next_free,
                              header_ptr->prev_free));
256

257 258 259 260 261
                header_ptr->next_free->prev_free = header_ptr->prev_free;
                header_ptr->prev_free->next_free = header_ptr->next_free;
                header_ptr->next_free = RT_NULL;
                header_ptr->prev_free = RT_NULL;
            }
262

263 264
            /* Mark the allocated block as not available. */
            header_ptr->magic |= RT_MEMHEAP_USED;
265

266 267 268
            /* release lock */
            rt_sem_release(&(heap->lock));

269 270
            /* Return a memory address to the caller.  */
            RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
271
                         ("alloc mem: memory[0x%08x], heap[0x%08x], size: %d\n",
272 273
                          (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE),
                          header_ptr,
274
                          size));
275

276
            return (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE);
277
        }
278

279
        /* release lock */
280
        rt_sem_release(&(heap->lock));
281
    }
282

283
    RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate memory: failed\n"));
284 285 286 287

    /* Return the completion status.  */
    return RT_NULL;
}
288
RTM_EXPORT(rt_memheap_alloc);
289

Y
yiyue.fang 已提交
290
void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
B
Bernard Xiong 已提交
291
{
B
Bernard Xiong 已提交
292
    rt_err_t result;
Y
yiyue.fang 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    rt_size_t oldsize;
    struct rt_memheap_item *header_ptr;
    struct rt_memheap_item *new_ptr;

    if (newsize == 0)
    {
        rt_memheap_free(ptr);

        return RT_NULL;
    }
    /* align allocated size */
    newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
    if (newsize < RT_MEMHEAP_MINIALLOC)
        newsize = RT_MEMHEAP_MINIALLOC;

    if (ptr == RT_NULL)
    {
        return rt_memheap_alloc(heap, newsize);
    }

    /* get memory block header and get the size of memory block */
    header_ptr = (struct rt_memheap_item *)
                 ((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
    oldsize = MEMITEM_SIZE(header_ptr);
B
Bernard Xiong 已提交
317 318 319
     /* re-allocate memory */
    if (newsize > oldsize)
    {
Y
yiyue.fang 已提交
320
        void* new_ptr;
B
Bernard Xiong 已提交
321 322 323 324 325 326 327
        /* re-allocate a memory block */
        new_ptr = (void*)rt_memheap_alloc(heap, newsize);
        if (new_ptr != RT_NULL)
        {
            rt_memcpy(new_ptr, ptr, oldsize < newsize ? oldsize : newsize);
            rt_memheap_free(ptr);
        }
B
Bernard Xiong 已提交
328

B
Bernard Xiong 已提交
329 330 331
        return new_ptr;
    }

332 333 334 335
    /* don't split when there is less than one node space left */
    if (newsize + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC >= oldsize)
        return ptr;

Y
yiyue.fang 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349
    /* lock memheap */
    result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
    if (result != RT_EOK)
    {
        rt_set_errno(result);

        return RT_NULL;
    }

    /* split the block. */
    new_ptr = (struct rt_memheap_item *)
              (((rt_uint8_t *)header_ptr) + newsize + RT_MEMHEAP_SIZE);

    RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
350
                 ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
Y
yiyue.fang 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
                  header_ptr,
                  header_ptr->next,
                  header_ptr->prev,
                  new_ptr));

    /* mark the new block as a memory block and freed. */
    new_ptr->magic = RT_MEMHEAP_MAGIC;
    /* put the pool pointer into the new block. */
    new_ptr->pool_ptr = heap;

    /* break down the block list */
    new_ptr->prev          = header_ptr;
    new_ptr->next          = header_ptr->next;
    header_ptr->next->prev = new_ptr;
    header_ptr->next       = new_ptr;

    /* determine if the block can be merged with the next neighbor. */
    if (!RT_MEMHEAP_IS_USED(new_ptr->next))
    {
        struct rt_memheap_item *free_ptr;

        /* merge block with next neighbor. */
        free_ptr = new_ptr->next;
        heap->available_size = heap->available_size - MEMITEM_SIZE(free_ptr);

        RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
377
                     ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
Y
yiyue.fang 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
                      header_ptr, header_ptr->next_free, header_ptr->prev_free));

        free_ptr->next->prev = new_ptr;
        new_ptr->next   = free_ptr->next;

        /* remove free ptr from free list */
        free_ptr->next_free->prev_free = free_ptr->prev_free;
        free_ptr->prev_free->next_free = free_ptr->next_free;
    }

    /* insert the split block to free list */
    new_ptr->next_free = heap->free_list->next_free;
    new_ptr->prev_free = heap->free_list;
    heap->free_list->next_free->prev_free = new_ptr;
    heap->free_list->next_free            = new_ptr;
393
    RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new free ptr: next_free 0x%08x, prev_free 0x%08x\n",
Y
yiyue.fang 已提交
394 395 396 397 398
                                    new_ptr->next_free,
                                    new_ptr->prev_free));

    /* increment the available byte count.  */
    heap->available_size = heap->available_size + MEMITEM_SIZE(new_ptr);
B
Bernard Xiong 已提交
399 400 401 402

    /* release lock */
    rt_sem_release(&(heap->lock));

Y
yiyue.fang 已提交
403 404
    /* return the old memory block */
    return ptr;
B
Bernard Xiong 已提交
405 406 407
}
RTM_EXPORT(rt_memheap_realloc);

408
void rt_memheap_free(void *ptr)
409
{
410 411 412 413 414
    rt_err_t result;
    struct rt_memheap *heap;
    struct rt_memheap_item *header_ptr, *new_ptr;
    rt_uint32_t insert_header;

415 416 417
	/* NULL check */
	if (ptr == RT_NULL) return;

418 419 420
    /* set initial status as OK */
    insert_header = 1;
    new_ptr       = RT_NULL;
Y
yiyue.fang 已提交
421 422
    header_ptr    = (struct rt_memheap_item *)
                    ((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);
423

424
    RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("free memory: memory[0x%08x], block[0x%08x]\n",
425
                                    ptr, header_ptr));
426

427 428 429 430 431
    /* check magic */
    RT_ASSERT((header_ptr->magic & RT_MEMHEAP_MASK) == RT_MEMHEAP_MAGIC);

    /* get pool ptr */
    heap = header_ptr->pool_ptr;
432

433
    /* lock memheap */
434
    result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
435 436 437
    if (result != RT_EOK)
    {
        rt_set_errno(result);
Y
yiyue.fang 已提交
438

439 440
        return ;
    }
441

442 443 444
    /* Mark the memory as available. */
    header_ptr->magic &= ~RT_MEMHEAP_USED;
    /* Adjust the available number of bytes. */
445
    heap->available_size = heap->available_size + MEMITEM_SIZE(header_ptr);
446

447 448 449
    /* Determine if the block can be merged with the previous neighbor. */
    if (!RT_MEMHEAP_IS_USED(header_ptr->prev))
    {
450
        RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: left node 0x%08x\n",
451
                                        header_ptr->prev));
452

453 454
        /* adjust the available number of bytes. */
        heap->available_size = heap->available_size + RT_MEMHEAP_SIZE;
455

456 457 458
        /* yes, merge block with previous neighbor. */
        (header_ptr->prev)->next = header_ptr->next;
        (header_ptr->next)->prev = header_ptr->prev;
459

460 461 462 463 464
        /* move header pointer to previous. */
        header_ptr = header_ptr->prev;
        /* don't insert header to free list */
        insert_header = 0;
    }
465

466 467 468 469 470
    /* determine if the block can be merged with the next neighbor. */
    if (!RT_MEMHEAP_IS_USED(header_ptr->next))
    {
        /* adjust the available number of bytes. */
        heap->available_size = heap->available_size + RT_MEMHEAP_SIZE;
471

472 473
        /* merge block with next neighbor. */
        new_ptr = header_ptr->next;
474

475
        RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
476
                     ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
477
                      new_ptr, new_ptr->next_free, new_ptr->prev_free));
478

479 480
        new_ptr->next->prev = header_ptr;
        header_ptr->next    = new_ptr->next;
481

482 483 484 485
        /* remove new ptr from free list */
        new_ptr->next_free->prev_free = new_ptr->prev_free;
        new_ptr->prev_free->next_free = new_ptr->next_free;
    }
486

487 488 489 490 491 492 493
    if (insert_header)
    {
        /* no left merge, insert to free list */
        header_ptr->next_free = heap->free_list->next_free;
        header_ptr->prev_free = heap->free_list;
        heap->free_list->next_free->prev_free = header_ptr;
        heap->free_list->next_free            = header_ptr;
494

495
        RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
496
                     ("insert to free list: next_free 0x%08x, prev_free 0x%08x\n",
497
                      header_ptr->next_free, header_ptr->prev_free));
498
    }
499

500
    /* release lock */
501
    rt_sem_release(&(heap->lock));
502
}
503
RTM_EXPORT(rt_memheap_free);
504

505 506 507 508 509
#ifdef RT_USING_MEMHEAP_AS_HEAP
static struct rt_memheap _heap;

void rt_system_heap_init(void *begin_addr, void *end_addr)
{
510 511 512 513 514
    /* initialize a default heap in the system */
    rt_memheap_init(&_heap,
                    "heap",
                    begin_addr,
                    (rt_uint32_t)end_addr - (rt_uint32_t)begin_addr);
515 516 517 518
}

void *rt_malloc(rt_size_t size)
{
519
    void* ptr;
B
Bernard Xiong 已提交
520

521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    /* try to allocate in system heap */
    ptr = rt_memheap_alloc(&_heap, size);
    if (ptr == RT_NULL)
    {
        struct rt_object *object;
        struct rt_list_node *node;
        struct rt_memheap *heap;
        struct rt_object_information *information;
        extern struct rt_object_information rt_object_container[];

        /* try to allocate on other memory heap */
        information = &rt_object_container[RT_Object_Class_MemHeap];
        for (node  = information->object_list.next;
             node != &(information->object_list);
             node  = node->next)
        {
            object = rt_list_entry(node, struct rt_object, list);
            heap   = (struct rt_memheap *)object;

            /* not allocate in the default system heap */
            if (heap == &_heap)
                continue;

            ptr = rt_memheap_alloc(heap, size);
            if (ptr != RT_NULL)
                break;
        }
    }

    return ptr;
551 552 553 554 555
}
RTM_EXPORT(rt_malloc);

void rt_free(void *rmem)
{
556
    rt_memheap_free(rmem);
557 558 559 560 561
}
RTM_EXPORT(rt_free);

void *rt_realloc(void *rmem, rt_size_t newsize)
{
Y
yiyue.fang 已提交
562
    void *new_ptr;
563 564
    struct rt_memheap_item *header_ptr;

Y
yiyue.fang 已提交
565 566
    if (rmem == RT_NULL)
        return rt_malloc(newsize);
567 568

    /* get old memory item */
Y
yiyue.fang 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    header_ptr = (struct rt_memheap_item *)
                 ((rt_uint8_t *)rmem - RT_MEMHEAP_SIZE);

    new_ptr = rt_memheap_realloc(header_ptr->pool_ptr, rmem, newsize);
    if (new_ptr == RT_NULL && newsize != 0)
    {
        /* allocate memory block from other memheap */
        new_ptr = rt_malloc(newsize);
        if (new_ptr != RT_NULL && rmem != RT_NULL)
        {
            rt_size_t oldsize;

            /* get the size of old memory block */
            oldsize = MEMITEM_SIZE(header_ptr);
            if (newsize > oldsize)
                rt_memcpy(new_ptr, rmem, oldsize);
            else
                rt_memcpy(new_ptr, rmem, newsize);
        }
    }

    return new_ptr;
591 592 593 594 595
}
RTM_EXPORT(rt_realloc);

void *rt_calloc(rt_size_t count, rt_size_t size)
{
596 597 598 599 600 601 602 603 604 605 606 607
    void *ptr;
    rt_size_t total_size;

    total_size = count * size;
    ptr = rt_malloc(total_size);
    if (ptr != RT_NULL)
    {
        /* clean memory */
        rt_memset(ptr, 0, total_size);
    }

    return ptr;
608 609 610 611 612
}
RTM_EXPORT(rt_calloc);

#endif

613
#endif