unicstl/include/rawbuf.h
wjf-hs 0236e6e315 feat(rawbuf): 新增 rawbuf 模块,做随机访问,支持malloc和静态内存。
(之前我突然对darray理解错了,现在的darray设计就是合理的。注意区分开rawbuf和darray)
2026-05-15 15:53:11 +08:00

44 lines
1.1 KiB
C

/**
* @file rawbuf.c
* @author wenjf (orig5826@163.com)
* @brief
* @version 0.1
* @date 2026-05-15
*
* @copyright Copyright (c) 2026
*
*/
#ifndef _RAWBUF_H_
#define _RAWBUF_H_
#include "unicstl_internal.h"
struct _rawbuf
{
// -------------------- private --------------------
void *obj;
size_t _obj_size;
size_t _capacity;
bool _dynamic;
void (*_destory)(struct _rawbuf *self);
// -------------------- public --------------------
// kernel -> random access
bool (*set)(struct _rawbuf *self, size_t index, const void *obj); // O(1)
bool (*get)(struct _rawbuf *self, size_t index, void *obj); // O(1)
const void* (*at)(struct _rawbuf *self, size_t index); // O(1)
// base
size_t (*capacity)(struct _rawbuf *self);
};
typedef struct _rawbuf *rawbuf_t;
rawbuf_t rawbuf_new(size_t obj_size, size_t capacity);
void rawbuf_free(rawbuf_t *rawbuf);
#ifdef UNICSTL_STATIC_MEMORY
bool rawbuf_init(struct _rawbuf *self, size_t obj_size, size_t capacity, void *mem_base);
#endif
#endif