1 /****************************************************************************** 2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") 3 * All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 *****************************************************************************/ 18 #pragma once 19 20 #include "types.h" 21 typedef struct mem_block_t { 22 struct mem_block_t *next_block; 23 char data[4]; // must 4 or 8 aligned, padding 24 } mem_block_t; 25 26 typedef struct mem_pool_t { 27 mem_block_t *free_list; 28 } mem_pool_t; 29 30 #define MEMPOOL_ALIGNMENT 4 31 #define MEMPOOL_ITEMSIZE_2_BLOCKSIZE(s) (((s) + (MEMPOOL_ALIGNMENT - 1)) & ~(MEMPOOL_ALIGNMENT - 1)) 32 33 #define MEMPOOL_DECLARE(pool_name, pool_mem, itemsize, itemcount) \ 34 mem_pool_t pool_name; \ 35 u8 pool_mem[MEMPOOL_ITEMSIZE_2_BLOCKSIZE(itemsize) * (itemcount)] _attribute_aligned_(4) 36 37 mem_pool_t *mempool_init(mem_pool_t *pool, void *mem, int itemsize, int itemcount); 38 void *mempool_alloc(mem_pool_t *pool); 39 void mempool_free(mem_pool_t *pool, void *p); 40 mem_block_t *mempool_header(char *pd); 41