sw_perf/byte_alloc.h

51 lines
1.4 KiB
C

/*********************************************************************************************
Description: Byte memory allocation with boundary aligned
Copyright : All right reserved by NCIC.ICT
Author : Zhang Zhonghai
Date : 2023/08/23
***********************************************************************************************/
#ifndef __MEMORY_H
#define __MEMORY_H
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define MEM_ALIGN_BYTE 8
#define MEM_MOVE_BIT 3
typedef struct _byte_mem_t
{
size_t occupied; // 已经占用的容量(字节数) 对齐的
size_t capacity; // 总容量(字节数)
void *mem; // 申请的内存首地址
} byte_mem_t;
// 创建byte_mem_t
byte_mem_t *create_byte_mem();
// 初始化
void init_byte_mem(byte_mem_t *bmem);
// 初始化并开辟一定量的内存
void byte_mem_init_alloc(byte_mem_t *bmem, size_t byte_cnt);
// 请求内存
void *byte_mem_request(byte_mem_t *bmem, size_t byte_cnt);
// 请求内存并初始化为零
void *byte_mem_request_and_clean(byte_mem_t *bmem, size_t byte_cnt);
// 将不用的内存归还给byte mem
void byte_mem_release(byte_mem_t *bmem, size_t byte_cnt);
// 清空占用量
void byte_mem_clear(byte_mem_t *bmem);
// 彻底释放内存
void byte_mem_free(byte_mem_t *bmem);
// 销毁byte_mem_t
void destroy_byte_mem(byte_mem_t *bmem);
#endif