mempool.c 12.4 KB
Newer Older
1
/*
B
bernard.xiong 已提交
2
 * File      : mempool.c
3
 * This file is part of RT-Thread RTOS
D
dzzxzz 已提交
4
 * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
5
 *
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
 * 2006-05-27     Bernard      implement memory pool
B
bernard.xiong 已提交
23
 * 2006-06-03     Bernard      fix the thread timer init bug
24 25 26
 * 2006-06-30     Bernard      fix the allocate/free block bug
 * 2006-08-04     Bernard      add hook support
 * 2006-08-10     Bernard      fix interrupt bug in rt_mp_alloc
27
 * 2010-07-13     Bernard      fix RT_ALIGN issue found by kuronca
qiuyiuestc's avatar
qiuyiuestc 已提交
28
 * 2010-10-26     yi.qiu       add module support in rt_mp_delete
29
 * 2011-01-24     Bernard      add object allocation check.
30
 * 2012-03-22     Bernard      fix align issue in rt_mp_init and rt_mp_create.
31 32 33 34 35 36 37 38
 */

#include <rthw.h>
#include <rtthread.h>

#ifdef RT_USING_MEMPOOL

#ifdef RT_USING_HOOK
D
dzzxzz 已提交
39 40
static void (*rt_mp_alloc_hook)(struct rt_mempool *mp, void *block);
static void (*rt_mp_free_hook)(struct rt_mempool *mp, void *block);
41 42 43 44

/**
 * @addtogroup Hook
 */
D
dzzxzz 已提交
45

46 47 48 49 50
/*@{*/

/**
 * This function will set a hook function, which will be invoked when a memory
 * block is allocated from memory pool.
B
bernard.xiong 已提交
51
 *
52 53
 * @param hook the hook function
 */
D
dzzxzz 已提交
54
void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block))
55
{
56
    rt_mp_alloc_hook = hook;
57 58 59 60 61
}

/**
 * This function will set a hook function, which will be invoked when a memory
 * block is released to memory pool.
B
bernard.xiong 已提交
62
 *
63 64
 * @param hook the hook function
 */
D
dzzxzz 已提交
65
void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block))
66
{
67
    rt_mp_free_hook = hook;
68 69 70 71 72 73 74 75 76 77 78 79
}

/*@}*/
#endif

/**
 * @addtogroup MM
 */

/*@{*/

/**
80 81
 * This function will initialize a memory pool object, normally which is used
 * for static object.
82
 *
B
bernard.xiong@gmail.com 已提交
83
 * @param mp the memory pool object
84 85 86 87 88
 * @param name the name of memory pool
 * @param start the star address of memory pool
 * @param size the total size of memory pool
 * @param block_size the size for each block
 *
B
bernard.xiong@gmail.com 已提交
89
 * @return RT_EOK
90
 */
91 92 93 94 95
rt_err_t rt_mp_init(struct rt_mempool *mp,
                    const char        *name,
                    void              *start,
                    rt_size_t          size,
                    rt_size_t          block_size)
96
{
97 98
    rt_uint8_t *block_ptr;
    register rt_base_t offset;
99

100 101
    /* parameter check */
    RT_ASSERT(mp != RT_NULL);
102

103 104
    /* initialize object */
    rt_object_init(&(mp->parent), RT_Object_Class_MemPool, name);
105

106 107 108
    /* initialize memory pool */
    mp->start_address = start;
    mp->size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
109

110 111 112
    /* align the block size */
    block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
    mp->block_size = block_size;
113

114 115 116
    /* align to align size byte */
    mp->block_total_count = mp->size / (mp->block_size + sizeof(rt_uint8_t *));
    mp->block_free_count  = mp->block_total_count;
117

118 119 120
    /* initialize suspended thread list */
    rt_list_init(&(mp->suspend_thread));
    mp->suspend_thread_count = 0;
121

122 123 124 125 126 127 128
    /* initialize free block list */
    block_ptr = (rt_uint8_t *)mp->start_address;
    for (offset = 0; offset < mp->block_total_count; offset ++)
    {
        *(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *))) =
            (rt_uint8_t *)(block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *)));
    }
129

130 131
    *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *))) =
        RT_NULL;
132

133
    mp->block_list = block_ptr;
134

135
    return RT_EOK;
136
}
137
RTM_EXPORT(rt_mp_init);
138

B
bernard.xiong@gmail.com 已提交
139 140 141 142 143 144 145
/**
 * This function will detach a memory pool from system object management.
 *
 * @param mp the memory pool object
 *
 * @return RT_EOK
 */
D
dzzxzz 已提交
146
rt_err_t rt_mp_detach(struct rt_mempool *mp)
147
{
148 149
    struct rt_thread *thread;
    register rt_ubase_t temp;
150

151 152
    /* parameter check */
    RT_ASSERT(mp != RT_NULL);
153

154 155 156 157 158
    /* wake up all suspended threads */
    while (!rt_list_isempty(&(mp->suspend_thread)))
    {
        /* disable interrupt */
        temp = rt_hw_interrupt_disable();
159

160 161 162 163
        /* get next suspend thread */
        thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
        /* set error code to RT_ERROR */
        thread->error = -RT_ERROR;
164

165 166 167 168 169 170
        /*
         * resume thread
         * In rt_thread_resume function, it will remove current thread from
         * suspend list
         */
        rt_thread_resume(thread);
171

172 173
        /* decrease suspended thread count */
        mp->suspend_thread_count --;
174

175 176 177
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
    }
178

179 180
    /* detach object */
    rt_object_detach(&(mp->parent));
181

182
    return RT_EOK;
183
}
184
RTM_EXPORT(rt_mp_detach);
185 186 187

#ifdef RT_USING_HEAP
/**
188 189
 * This function will create a mempool object and allocate the memory pool from
 * heap.
190 191 192 193 194 195 196
 *
 * @param name the name of memory pool
 * @param block_count the count of blocks in memory pool
 * @param block_size the size for each block
 *
 * @return the created mempool object
 */
197 198 199
rt_mp_t rt_mp_create(const char *name,
                     rt_size_t   block_count,
                     rt_size_t   block_size)
200
{
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    rt_uint8_t *block_ptr;
    struct rt_mempool *mp;
    register rt_base_t offset;

    RT_DEBUG_NOT_IN_INTERRUPT;

    /* allocate object */
    mp = (struct rt_mempool *)rt_object_allocate(RT_Object_Class_MemPool, name);
    /* allocate object failed */
    if (mp == RT_NULL)
        return RT_NULL;

    /* initialize memory pool */
    block_size     = RT_ALIGN(block_size, RT_ALIGN_SIZE);
    mp->block_size = block_size;
    mp->size       = (block_size + sizeof(rt_uint8_t *)) * block_count;

    /* allocate memory */
    mp->start_address = rt_malloc((block_size + sizeof(rt_uint8_t *)) *
                                  block_count);
    if (mp->start_address == RT_NULL)
    {
        /* no memory, delete memory pool object */
        rt_object_delete(&(mp->parent));

        return RT_NULL;
    }

    mp->block_total_count = block_count;
    mp->block_free_count  = mp->block_total_count;

    /* initialize suspended thread list */
    rt_list_init(&(mp->suspend_thread));
    mp->suspend_thread_count = 0;

    /* initialize free block list */
    block_ptr = (rt_uint8_t *)mp->start_address;
    for (offset = 0; offset < mp->block_total_count; offset ++)
    {
        *(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *)))
            = block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *));
    }

    *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *)))
        = RT_NULL;

    mp->block_list = block_ptr;

    return mp;
250
}
251
RTM_EXPORT(rt_mp_create);
252 253 254 255 256 257

/**
 * This function will delete a memory pool and release the object memory.
 *
 * @param mp the memory pool object
 *
B
bernard.xiong@gmail.com 已提交
258
 * @return RT_EOK
259 260 261
 */
rt_err_t rt_mp_delete(rt_mp_t mp)
{
262 263
    struct rt_thread *thread;
    register rt_ubase_t temp;
264

265
    RT_DEBUG_NOT_IN_INTERRUPT;
266

267 268
    /* parameter check */
    RT_ASSERT(mp != RT_NULL);
269

270 271 272 273 274
    /* wake up all suspended threads */
    while (!rt_list_isempty(&(mp->suspend_thread)))
    {
        /* disable interrupt */
        temp = rt_hw_interrupt_disable();
275

276 277 278 279
        /* get next suspend thread */
        thread = rt_list_entry(mp->suspend_thread.next, struct rt_thread, tlist);
        /* set error code to RT_ERROR */
        thread->error = -RT_ERROR;
280

281 282 283 284 285 286
        /*
         * resume thread
         * In rt_thread_resume function, it will remove current thread from
         * suspend list
         */
        rt_thread_resume(thread);
287

288 289
        /* decrease suspended thread count */
        mp->suspend_thread_count --;
290

291 292 293
        /* enable interrupt */
        rt_hw_interrupt_enable(temp);
    }
294

qiuyiuestc's avatar
qiuyiuestc 已提交
295
#if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
296
    /* the mp object belongs to an application module */
B
Bernard Xiong 已提交
297
    if (mp->parent.flag & RT_OBJECT_FLAG_MODULE)
298 299
        rt_module_free(mp->parent.module_id, mp->start_address);
    else
qiuyiuestc's avatar
qiuyiuestc 已提交
300 301
#endif

302 303
    /* release allocated room */
    rt_free(mp->start_address);
B
bernard.xiong 已提交
304

305 306
    /* detach object */
    rt_object_delete(&(mp->parent));
307

308
    return RT_EOK;
309
}
310
RTM_EXPORT(rt_mp_delete);
311 312 313 314 315 316 317 318
#endif

/**
 * This function will allocate a block from memory pool
 *
 * @param mp the memory pool object
 * @param time the waiting time
 *
B
bernard.xiong@gmail.com 已提交
319
 * @return the allocated memory block or RT_NULL on allocated failed
320
 */
D
dzzxzz 已提交
321
void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
322
{
323 324 325
    rt_uint8_t *block_ptr;
    register rt_base_t level;
    struct rt_thread *thread;
326 327 328 329
    rt_uint32_t before_sleep = 0;

    /* get current thread */
    thread = rt_thread_self();
330 331 332 333

    /* disable interrupt */
    level = rt_hw_interrupt_disable();

334
    while (mp->block_free_count == 0)
335 336 337 338 339 340 341
    {
        /* memory block is unavailable. */
        if (time == 0)
        {
            /* enable interrupt */
            rt_hw_interrupt_enable(level);

342 343
            rt_set_errno(-RT_ETIMEOUT);

344 345 346
            return RT_NULL;
        }

347
        RT_DEBUG_NOT_IN_INTERRUPT;
348

349
        thread->error = RT_EOK;
350

351 352 353 354
        /* need suspend thread */
        rt_thread_suspend(thread);
        rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist));
        mp->suspend_thread_count++;
355

356 357 358 359 360 361 362 363 364 365 366
        if (time > 0)
        {
            /* get the start tick of timer */
            before_sleep = rt_tick_get();

            /* init thread timer and start it */
            rt_timer_control(&(thread->thread_timer),
                             RT_TIMER_CTRL_SET_TIME,
                             &time);
            rt_timer_start(&(thread->thread_timer));
        }
367

368 369
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
370

371 372
        /* do a schedule */
        rt_schedule();
373

374 375 376 377 378 379 380 381 382 383 384 385
        if (thread->error != RT_EOK)
            return RT_NULL;

        if (time > 0)
        {
            time -= rt_tick_get() - before_sleep;
            if (time < 0)
                time = 0;
        }
        /* disable interrupt */
        level = rt_hw_interrupt_disable();
    }
386

387 388
    /* memory block is available. decrease the free block counter */
    mp->block_free_count--;
389

390 391 392
    /* get block from block list */
    block_ptr = mp->block_list;
    RT_ASSERT(block_ptr != RT_NULL);
393

394 395
    /* Setup the next free node. */
    mp->block_list = *(rt_uint8_t **)block_ptr;
396

397 398
    /* point to memory pool */
    *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
399 400 401 402 403 404 405 406

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    RT_OBJECT_HOOK_CALL(rt_mp_alloc_hook,
                        (mp, (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *))));

    return (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *));
407
}
408
RTM_EXPORT(rt_mp_alloc);
409 410 411 412 413 414

/**
 * This function will release a memory block
 *
 * @param block the address of memory block to be released
 */
D
dzzxzz 已提交
415
void rt_mp_free(void *block)
416
{
417 418 419 420
    rt_uint8_t **block_ptr;
    struct rt_mempool *mp;
    struct rt_thread *thread;
    register rt_base_t level;
421

422 423 424
    /* get the control block of pool which the block belongs to */
    block_ptr = (rt_uint8_t **)((rt_uint8_t *)block - sizeof(rt_uint8_t *));
    mp        = (struct rt_mempool *)*block_ptr;
425

426
    RT_OBJECT_HOOK_CALL(rt_mp_free_hook, (mp, block));
427

428 429
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
430

431 432
    /* increase the free block count */
    mp->block_free_count ++;
433

434 435 436
    /* link the block into the block list */
    *block_ptr = mp->block_list;
    mp->block_list = (rt_uint8_t *)block_ptr;
437

438 439 440 441 442 443
    if (mp->suspend_thread_count > 0)
    {
        /* get the suspended thread */
        thread = rt_list_entry(mp->suspend_thread.next,
                               struct rt_thread,
                               tlist);
444

445 446
        /* set error */
        thread->error = RT_EOK;
447

448 449
        /* resume thread */
        rt_thread_resume(thread);
450

451 452
        /* decrease suspended thread count */
        mp->suspend_thread_count --;
453

454 455
        /* enable interrupt */
        rt_hw_interrupt_enable(level);
456

457 458
        /* do a schedule */
        rt_schedule();
459

460 461
        return;
    }
462

463 464
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
465
}
466
RTM_EXPORT(rt_mp_free);
467 468 469

/*@}*/

D
dzzxzz 已提交
470 471
#endif