Mouse code for the MacroRat
mbed-dev/targets/TARGET_Silicon_Labs/TARGET_SL_RAIL/efr32-rf-driver/buffer-pool-memory-manager/buffer_pool_allocator.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /***************************************************************************//** |
sahilmgandhi | 18:6a4db94011d3 | 2 | * @file buffer_pool_allocator.c |
sahilmgandhi | 18:6a4db94011d3 | 3 | * @brief The source for a simple memory allocator that statically creates pools |
sahilmgandhi | 18:6a4db94011d3 | 4 | * of fixed size buffers to allocate from. |
sahilmgandhi | 18:6a4db94011d3 | 5 | * @copyright Copyright 2015 Silicon Laboratories, Inc. http://www.silabs.com |
sahilmgandhi | 18:6a4db94011d3 | 6 | ******************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 7 | |
sahilmgandhi | 18:6a4db94011d3 | 8 | #include <stdlib.h> |
sahilmgandhi | 18:6a4db94011d3 | 9 | |
sahilmgandhi | 18:6a4db94011d3 | 10 | #include "buffer_pool_allocator.h" |
sahilmgandhi | 18:6a4db94011d3 | 11 | |
sahilmgandhi | 18:6a4db94011d3 | 12 | #include "em_core.h" |
sahilmgandhi | 18:6a4db94011d3 | 13 | |
sahilmgandhi | 18:6a4db94011d3 | 14 | #ifdef CONFIGURATION_HEADER |
sahilmgandhi | 18:6a4db94011d3 | 15 | #include CONFIGURATION_HEADER |
sahilmgandhi | 18:6a4db94011d3 | 16 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 17 | |
sahilmgandhi | 18:6a4db94011d3 | 18 | // ----------------------------------------------------------------------------- |
sahilmgandhi | 18:6a4db94011d3 | 19 | // Configuration Macros |
sahilmgandhi | 18:6a4db94011d3 | 20 | // ----------------------------------------------------------------------------- |
sahilmgandhi | 18:6a4db94011d3 | 21 | |
sahilmgandhi | 18:6a4db94011d3 | 22 | #ifndef BUFFER_POOL_SIZE |
sahilmgandhi | 18:6a4db94011d3 | 23 | #define BUFFER_POOL_SIZE 8 |
sahilmgandhi | 18:6a4db94011d3 | 24 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 25 | #ifndef MAX_BUFFER_SIZE |
sahilmgandhi | 18:6a4db94011d3 | 26 | #define MAX_BUFFER_SIZE 160 |
sahilmgandhi | 18:6a4db94011d3 | 27 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 28 | |
sahilmgandhi | 18:6a4db94011d3 | 29 | #define INVALID_BUFFER_OBJ ((void*)0xFFFFFFFF) |
sahilmgandhi | 18:6a4db94011d3 | 30 | |
sahilmgandhi | 18:6a4db94011d3 | 31 | typedef struct { |
sahilmgandhi | 18:6a4db94011d3 | 32 | uint8_t refCount; |
sahilmgandhi | 18:6a4db94011d3 | 33 | uint8_t data[MAX_BUFFER_SIZE]; |
sahilmgandhi | 18:6a4db94011d3 | 34 | } BufferPoolObj_t; |
sahilmgandhi | 18:6a4db94011d3 | 35 | |
sahilmgandhi | 18:6a4db94011d3 | 36 | static BufferPoolObj_t memoryObjs[BUFFER_POOL_SIZE]; |
sahilmgandhi | 18:6a4db94011d3 | 37 | |
sahilmgandhi | 18:6a4db94011d3 | 38 | void* memoryAllocate(uint32_t size) |
sahilmgandhi | 18:6a4db94011d3 | 39 | { |
sahilmgandhi | 18:6a4db94011d3 | 40 | uint32_t i = 0; |
sahilmgandhi | 18:6a4db94011d3 | 41 | void *handle = INVALID_BUFFER_OBJ; |
sahilmgandhi | 18:6a4db94011d3 | 42 | |
sahilmgandhi | 18:6a4db94011d3 | 43 | // We can't support sizes greater than the maximum heap buffer size |
sahilmgandhi | 18:6a4db94011d3 | 44 | if(size > MAX_BUFFER_SIZE) { |
sahilmgandhi | 18:6a4db94011d3 | 45 | return INVALID_BUFFER_OBJ; |
sahilmgandhi | 18:6a4db94011d3 | 46 | } |
sahilmgandhi | 18:6a4db94011d3 | 47 | |
sahilmgandhi | 18:6a4db94011d3 | 48 | CORE_DECLARE_IRQ_STATE; |
sahilmgandhi | 18:6a4db94011d3 | 49 | CORE_ENTER_CRITICAL(); |
sahilmgandhi | 18:6a4db94011d3 | 50 | for(i = 0; i < BUFFER_POOL_SIZE; i++) |
sahilmgandhi | 18:6a4db94011d3 | 51 | { |
sahilmgandhi | 18:6a4db94011d3 | 52 | if(memoryObjs[i].refCount == 0) |
sahilmgandhi | 18:6a4db94011d3 | 53 | { |
sahilmgandhi | 18:6a4db94011d3 | 54 | memoryObjs[i].refCount = 1; |
sahilmgandhi | 18:6a4db94011d3 | 55 | handle = (void*)i; |
sahilmgandhi | 18:6a4db94011d3 | 56 | break; |
sahilmgandhi | 18:6a4db94011d3 | 57 | } |
sahilmgandhi | 18:6a4db94011d3 | 58 | } |
sahilmgandhi | 18:6a4db94011d3 | 59 | CORE_EXIT_CRITICAL(); |
sahilmgandhi | 18:6a4db94011d3 | 60 | |
sahilmgandhi | 18:6a4db94011d3 | 61 | return handle; |
sahilmgandhi | 18:6a4db94011d3 | 62 | } |
sahilmgandhi | 18:6a4db94011d3 | 63 | |
sahilmgandhi | 18:6a4db94011d3 | 64 | void *memoryPtrFromHandle(void *handle) |
sahilmgandhi | 18:6a4db94011d3 | 65 | { |
sahilmgandhi | 18:6a4db94011d3 | 66 | void *ptr = NULL; |
sahilmgandhi | 18:6a4db94011d3 | 67 | |
sahilmgandhi | 18:6a4db94011d3 | 68 | // Make sure we were given a valid handle |
sahilmgandhi | 18:6a4db94011d3 | 69 | if((handle == INVALID_BUFFER_OBJ) || ((uint32_t)handle > BUFFER_POOL_SIZE)) |
sahilmgandhi | 18:6a4db94011d3 | 70 | { |
sahilmgandhi | 18:6a4db94011d3 | 71 | return NULL; |
sahilmgandhi | 18:6a4db94011d3 | 72 | } |
sahilmgandhi | 18:6a4db94011d3 | 73 | |
sahilmgandhi | 18:6a4db94011d3 | 74 | CORE_DECLARE_IRQ_STATE; |
sahilmgandhi | 18:6a4db94011d3 | 75 | CORE_ENTER_CRITICAL(); |
sahilmgandhi | 18:6a4db94011d3 | 76 | if(memoryObjs[(uint32_t)handle].refCount > 0) |
sahilmgandhi | 18:6a4db94011d3 | 77 | { |
sahilmgandhi | 18:6a4db94011d3 | 78 | ptr = memoryObjs[(uint32_t)handle].data; |
sahilmgandhi | 18:6a4db94011d3 | 79 | } |
sahilmgandhi | 18:6a4db94011d3 | 80 | CORE_EXIT_CRITICAL(); |
sahilmgandhi | 18:6a4db94011d3 | 81 | |
sahilmgandhi | 18:6a4db94011d3 | 82 | return ptr; |
sahilmgandhi | 18:6a4db94011d3 | 83 | } |
sahilmgandhi | 18:6a4db94011d3 | 84 | |
sahilmgandhi | 18:6a4db94011d3 | 85 | void memoryFree(void *handle) |
sahilmgandhi | 18:6a4db94011d3 | 86 | { |
sahilmgandhi | 18:6a4db94011d3 | 87 | CORE_DECLARE_IRQ_STATE; |
sahilmgandhi | 18:6a4db94011d3 | 88 | CORE_ENTER_CRITICAL(); |
sahilmgandhi | 18:6a4db94011d3 | 89 | if(memoryPtrFromHandle(handle) != NULL) |
sahilmgandhi | 18:6a4db94011d3 | 90 | { |
sahilmgandhi | 18:6a4db94011d3 | 91 | memoryObjs[(uint32_t)handle].refCount--; |
sahilmgandhi | 18:6a4db94011d3 | 92 | } |
sahilmgandhi | 18:6a4db94011d3 | 93 | CORE_EXIT_CRITICAL(); |
sahilmgandhi | 18:6a4db94011d3 | 94 | } |
sahilmgandhi | 18:6a4db94011d3 | 95 | |
sahilmgandhi | 18:6a4db94011d3 | 96 | void memoryTakeReference(void *handle) |
sahilmgandhi | 18:6a4db94011d3 | 97 | { |
sahilmgandhi | 18:6a4db94011d3 | 98 | CORE_DECLARE_IRQ_STATE; |
sahilmgandhi | 18:6a4db94011d3 | 99 | CORE_ENTER_CRITICAL(); |
sahilmgandhi | 18:6a4db94011d3 | 100 | if(memoryPtrFromHandle(handle) != NULL) |
sahilmgandhi | 18:6a4db94011d3 | 101 | { |
sahilmgandhi | 18:6a4db94011d3 | 102 | memoryObjs[(uint32_t)handle].refCount++; |
sahilmgandhi | 18:6a4db94011d3 | 103 | } |
sahilmgandhi | 18:6a4db94011d3 | 104 | CORE_EXIT_CRITICAL(); |
sahilmgandhi | 18:6a4db94011d3 | 105 | } |