add pool_ object

This commit is contained in:
lyon 2021-11-15 20:22:34 +08:00
parent 0f137eb528
commit 8e9ab7122e
2 changed files with 37 additions and 21 deletions

View File

@ -44,24 +44,36 @@ void pikaMemMaxReset(void) {
pikaMemInfo.heapUsedMax = 0;
}
PikaMemPool pool_init(uint32_t size) {
PikaMemPool pool;
pool.mem = __platformMalloc(size);
uint32_t pool_aline(Pool* pool, uint32_t size) {
return ((size - 1) / pool->aline + 1) * pool->aline;
}
Pool pool_init(uint32_t size, uint8_t aline) {
Pool pool;
pool.bitmap = bitmap_init(size);
pool.aline = aline;
pool.size = pool_aline(&pool, size);
pool.mem = __platformMalloc(pool_aline(&pool, pool.size));
return pool;
}
uint8_t* pool_malloc(PikaMemPool* pool, uint32_t size) {
void pool_deinit(Pool* pool) {
__platformFree(pool->mem);
pool->mem = NULL;
bitmap_deinit(pool->bitmap);
}
uint8_t* pool_malloc(Pool* pool, uint32_t size) {
return NULL;
}
void pool_free(PikaMemPool* pool, uint8_t* mem, uint32_t size) {
void pool_free(Pool* pool, uint8_t* mem, uint32_t size) {
return;
}
uint8_t* bitmap_init(uint32_t size) {
uint8_t* mem_bit_map =
(uint8_t*)__platformMalloc(((size - 1) / 8 + 1) * sizeof(char));
BitMap bitmap_init(uint32_t size) {
BitMap mem_bit_map =
(BitMap)__platformMalloc(((size - 1) / 8 + 1) * sizeof(char));
if (mem_bit_map == NULL)
NULL;
uint32_t size_mem_bit_map = (size - 1) / 8 + 1;
@ -69,7 +81,7 @@ uint8_t* bitmap_init(uint32_t size) {
return mem_bit_map;
}
void bitmap_set(uint8_t* bitmap, uint32_t index, uint8_t bit) {
void bitmap_set(BitMap bitmap, uint32_t index, uint8_t bit) {
uint32_t index_byte = index / 8;
uint8_t index_bit = index % 8;
uint8_t x = (0x1 << index_bit);
@ -83,7 +95,7 @@ void bitmap_set(uint8_t* bitmap, uint32_t index, uint8_t bit) {
return;
}
uint8_t bitmap_get(uint8_t* bitmap, uint32_t index) {
uint8_t bitmap_get(BitMap bitmap, uint32_t index) {
uint32_t index_byte = (index) / 8;
uint8_t index_bit = (index) % 8;
uint8_t x = (0x1 << index_bit);
@ -92,6 +104,6 @@ uint8_t bitmap_get(uint8_t* bitmap, uint32_t index) {
return bit > 0 ? 1 : 0;
}
void bitmap_deinit(uint8_t* bitmap) {
void bitmap_deinit(BitMap bitmap) {
__platformFree(bitmap);
}

View File

@ -16,10 +16,14 @@ typedef struct {
uint32_t heapUsedMax;
} PikaMemInfo;
typedef uint8_t* BitMap;
typedef struct {
uint8_t* bitmap;
BitMap bitmap;
uint8_t* mem;
} PikaMemPool;
uint8_t aline;
uint32_t size;
} Pool;
void pikaFree(void* mem, uint32_t size);
void* pikaMalloc(uint32_t size);
@ -27,13 +31,13 @@ uint16_t pikaMemNow(void);
uint16_t pikaMemMax(void);
void pikaMemMaxReset(void);
uint8_t* bitmap_init(uint32_t size);
void bitmap_set(uint8_t* bitmap, uint32_t index, uint8_t bit);
uint8_t bitmap_get(uint8_t* bitmap, uint32_t index);
void bitmap_deinit(uint8_t* bitmap);
PikaMemPool pool_init(uint32_t size);
uint8_t* pool_malloc(PikaMemPool* pool, uint32_t size);
void pool_free(PikaMemPool* pool, uint8_t* mem, uint32_t size);
BitMap bitmap_init(uint32_t size);
void bitmap_set(BitMap bitmap, uint32_t index, uint8_t bit);
uint8_t bitmap_get(BitMap bitmap, uint32_t index);
void bitmap_deinit(BitMap bitmap);
Pool pool_init(uint32_t size, uint8_t aline);
uint8_t* pool_malloc(Pool* pool, uint32_t size);
void pool_free(Pool* pool, uint8_t* mem, uint32_t size);
void pool_deinit(Pool* pool);
#endif