Files
2026-05-18 23:17:43 -07:00

760 lines
27 KiB
C

/* This module is used to test a use case of a module that stores information
* about keys in global memory, and relies on the enhanced data type callbacks to
* get key name and dbid on various operations.
*
* it simulates a simple memory allocator. The smallest allocation unit of
* the allocator is a mem block with a size of 4KB. Multiple mem blocks are combined
* using a linked list. These linked lists are placed in a global dict named 'mem_pool'.
* Each db has a 'mem_pool'. You can use the 'mem.alloc' command to allocate a specified
* number of mem blocks, and use 'mem.free' to release the memory. Use 'mem.write', 'mem.read'
* to write and read the specified mem block (note that each mem block can only be written once).
* Use 'mem.usage' to get the memory usage under different dbs, and it will return the size
* mem blocks and used mem blocks under the db.
* The specific structure diagram is as follows:
*
*
* Global variables of the module:
*
* mem blocks link
* ┌─────┬─────┐
* │ │ │ ┌───┐ ┌───┐ ┌───┐
* │ k1 │ ───┼───►│4KB├───►│4KB├───►│4KB│
* │ │ │ └───┘ └───┘ └───┘
* ├─────┼─────┤
* ┌───────┐ ┌────► │ │ │ ┌───┐ ┌───┐
* │ │ │ │ k2 │ ───┼───►│4KB├───►│4KB│
* │ db0 ├──────┘ │ │ │ └───┘ └───┘
* │ │ ├─────┼─────┤
* ├───────┤ │ │ │ ┌───┐ ┌───┐ ┌───┐
* │ │ │ k3 │ ───┼───►│4KB├───►│4KB├───►│4KB│
* │ db1 ├──►null │ │ │ └───┘ └───┘ └───┘
* │ │ └─────┴─────┘
* ├───────┤ dict
* │ │
* │ db2 ├─────────┐
* │ │ │
* ├───────┤ │ ┌─────┬─────┐
* │ │ │ │ │ │ ┌───┐ ┌───┐ ┌───┐
* │ db3 ├──►null │ │ k1 │ ───┼───►│4KB├───►│4KB├───►│4KB│
* │ │ │ │ │ │ └───┘ └───┘ └───┘
* └───────┘ │ ├─────┼─────┤
* mem_pool[MAX_DB] │ │ │ │ ┌───┐ ┌───┐
* └──►│ k2 │ ───┼───►│4KB├───►│4KB│
* │ │ │ └───┘ └───┘
* └─────┴─────┘
* dict
*
*
* Keys in server database:
*
* ┌───────┐
* │ size │
* ┌───────────►│ used │
* │ │ mask │
* ┌─────┬─────┐ │ └───────┘ ┌───────┐
* │ │ │ │ MemAllocObject │ size │
* │ k1 │ ───┼─┘ ┌───────────►│ used │
* │ │ │ │ │ mask │
* ├─────┼─────┤ ┌───────┐ ┌─────┬─────┐ │ └───────┘
* │ │ │ │ size │ │ │ │ │ MemAllocObject
* │ k2 │ ───┼─────────────►│ used │ │ k1 │ ───┼─┘
* │ │ │ │ mask │ │ │ │
* ├─────┼─────┤ └───────┘ ├─────┼─────┤
* │ │ │ MemAllocObject │ │ │
* │ k3 │ ───┼─┐ │ k2 │ ───┼─┐
* │ │ │ │ │ │ │ │
* └─────┴─────┘ │ ┌───────┐ └─────┴─────┘ │ ┌───────┐
* server db[0] │ │ size │ server db[1] │ │ size │
* └───────────►│ used │ └───────────►│ used │
* │ mask │ │ mask │
* └───────┘ └───────┘
* MemAllocObject MemAllocObject
*
**/
#include "kvmodule.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdint.h>
static KVModuleType *MemAllocType;
#define MAX_DB 16
KVModuleDict *mem_pool[MAX_DB];
typedef struct MemAllocObject {
long long size;
long long used;
uint64_t mask;
} MemAllocObject;
MemAllocObject *createMemAllocObject(void) {
MemAllocObject *o = KVModule_Calloc(1, sizeof(*o));
return o;
}
/*---------------------------- mem block apis ------------------------------------*/
#define BLOCK_SIZE 4096
struct MemBlock {
char block[BLOCK_SIZE];
struct MemBlock *next;
};
void MemBlockFree(struct MemBlock *head) {
if (head) {
struct MemBlock *block = head->next, *next;
KVModule_Free(head);
while (block) {
next = block->next;
KVModule_Free(block);
block = next;
}
}
}
struct MemBlock *MemBlockCreate(long long num) {
if (num <= 0) {
return NULL;
}
struct MemBlock *head = KVModule_Calloc(1, sizeof(struct MemBlock));
struct MemBlock *block = head;
while (--num) {
block->next = KVModule_Calloc(1, sizeof(struct MemBlock));
block = block->next;
}
return head;
}
long long MemBlockNum(const struct MemBlock *head) {
long long num = 0;
const struct MemBlock *block = head;
while (block) {
num++;
block = block->next;
}
return num;
}
size_t MemBlockWrite(struct MemBlock *head, long long block_index, const char *data, size_t size) {
size_t w_size = 0;
struct MemBlock *block = head;
while (block_index-- && block) {
block = block->next;
}
if (block) {
size = size > BLOCK_SIZE ? BLOCK_SIZE:size;
memcpy(block->block, data, size);
w_size += size;
}
return w_size;
}
int MemBlockRead(struct MemBlock *head, long long block_index, char *data, size_t size) {
size_t r_size = 0;
struct MemBlock *block = head;
while (block_index-- && block) {
block = block->next;
}
if (block) {
size = size > BLOCK_SIZE ? BLOCK_SIZE:size;
memcpy(data, block->block, size);
r_size += size;
}
return r_size;
}
void MemPoolFreeDb(KVModuleCtx *ctx, int dbid) {
KVModuleString *key;
void *tdata;
KVModuleDictIter *iter = KVModule_DictIteratorStartC(mem_pool[dbid], "^", NULL, 0);
while((key = KVModule_DictNext(ctx, iter, &tdata)) != NULL) {
MemBlockFree((struct MemBlock *)tdata);
}
KVModule_DictIteratorStop(iter);
KVModule_FreeDict(NULL, mem_pool[dbid]);
mem_pool[dbid] = KVModule_CreateDict(NULL);
}
struct MemBlock *MemBlockClone(const struct MemBlock *head) {
struct MemBlock *newhead = NULL;
if (head) {
newhead = KVModule_Calloc(1, sizeof(struct MemBlock));
memcpy(newhead->block, head->block, BLOCK_SIZE);
struct MemBlock *newblock = newhead;
const struct MemBlock *oldblock = head->next;
while (oldblock) {
newblock->next = KVModule_Calloc(1, sizeof(struct MemBlock));
newblock = newblock->next;
memcpy(newblock->block, oldblock->block, BLOCK_SIZE);
oldblock = oldblock->next;
}
}
return newhead;
}
/*---------------------------- event handler ------------------------------------*/
void swapDbCallback(KVModuleCtx *ctx, KVModuleEvent e, uint64_t sub, void *data) {
KVMODULE_NOT_USED(ctx);
KVMODULE_NOT_USED(e);
KVMODULE_NOT_USED(sub);
KVModuleSwapDbInfo *ei = data;
// swap
KVModuleDict *tmp = mem_pool[ei->dbnum_first];
mem_pool[ei->dbnum_first] = mem_pool[ei->dbnum_second];
mem_pool[ei->dbnum_second] = tmp;
}
void flushdbCallback(KVModuleCtx *ctx, KVModuleEvent e, uint64_t sub, void *data) {
KVMODULE_NOT_USED(ctx);
KVMODULE_NOT_USED(e);
int i;
KVModuleFlushInfo *fi = data;
KVModule_AutoMemory(ctx);
if (sub == KVMODULE_SUBEVENT_FLUSHDB_START) {
if (fi->dbnum != -1) {
MemPoolFreeDb(ctx, fi->dbnum);
} else {
for (i = 0; i < MAX_DB; i++) {
MemPoolFreeDb(ctx, i);
}
}
}
}
/*---------------------------- command implementation ------------------------------------*/
/* MEM.ALLOC key block_num */
int MemAlloc_RedisCommand(KVModuleCtx *ctx, KVModuleString **argv, int argc) {
KVModule_AutoMemory(ctx);
if (argc != 3) {
return KVModule_WrongArity(ctx);
}
long long block_num;
if ((KVModule_StringToLongLong(argv[2], &block_num) != KVMODULE_OK) || block_num <= 0) {
return KVModule_ReplyWithError(ctx, "ERR invalid block_num: must be a value greater than 0");
}
KVModuleKey *key = KVModule_OpenKey(ctx, argv[1], KVMODULE_READ | KVMODULE_WRITE);
int type = KVModule_KeyType(key);
if (type != KVMODULE_KEYTYPE_EMPTY && KVModule_ModuleTypeGetType(key) != MemAllocType) {
return KVModule_ReplyWithError(ctx, KVMODULE_ERRORMSG_WRONGTYPE);
}
MemAllocObject *o;
if (type == KVMODULE_KEYTYPE_EMPTY) {
o = createMemAllocObject();
KVModule_ModuleTypeSetValue(key, MemAllocType, o);
} else {
o = KVModule_ModuleTypeGetValue(key);
}
struct MemBlock *mem = MemBlockCreate(block_num);
KVModule_Assert(mem != NULL);
KVModule_DictSet(mem_pool[KVModule_GetSelectedDb(ctx)], argv[1], mem);
o->size = block_num;
o->used = 0;
o->mask = 0;
KVModule_ReplyWithLongLong(ctx, block_num);
KVModule_ReplicateVerbatim(ctx);
return KVMODULE_OK;
}
/* MEM.FREE key */
int MemFree_RedisCommand(KVModuleCtx *ctx, KVModuleString **argv, int argc) {
KVModule_AutoMemory(ctx);
if (argc != 2) {
return KVModule_WrongArity(ctx);
}
KVModuleKey *key = KVModule_OpenKey(ctx, argv[1], KVMODULE_READ);
int type = KVModule_KeyType(key);
if (type != KVMODULE_KEYTYPE_EMPTY && KVModule_ModuleTypeGetType(key) != MemAllocType) {
return KVModule_ReplyWithError(ctx, KVMODULE_ERRORMSG_WRONGTYPE);
}
int ret = 0;
MemAllocObject *o;
if (type == KVMODULE_KEYTYPE_EMPTY) {
KVModule_ReplyWithLongLong(ctx, ret);
return KVMODULE_OK;
} else {
o = KVModule_ModuleTypeGetValue(key);
}
int nokey;
struct MemBlock *mem = (struct MemBlock *)KVModule_DictGet(mem_pool[KVModule_GetSelectedDb(ctx)], argv[1], &nokey);
if (!nokey && mem) {
KVModule_DictDel(mem_pool[KVModule_GetSelectedDb(ctx)], argv[1], NULL);
MemBlockFree(mem);
o->used = 0;
o->size = 0;
o->mask = 0;
ret = 1;
}
KVModule_ReplyWithLongLong(ctx, ret);
KVModule_ReplicateVerbatim(ctx);
return KVMODULE_OK;
}
/* MEM.WRITE key block_index data */
int MemWrite_RedisCommand(KVModuleCtx *ctx, KVModuleString **argv, int argc) {
KVModule_AutoMemory(ctx);
if (argc != 4) {
return KVModule_WrongArity(ctx);
}
long long block_index;
if ((KVModule_StringToLongLong(argv[2], &block_index) != KVMODULE_OK) || block_index < 0) {
return KVModule_ReplyWithError(ctx, "ERR invalid block_index: must be a value greater than 0");
}
KVModuleKey *key = KVModule_OpenKey(ctx, argv[1], KVMODULE_READ | KVMODULE_WRITE);
int type = KVModule_KeyType(key);
if (type != KVMODULE_KEYTYPE_EMPTY && KVModule_ModuleTypeGetType(key) != MemAllocType) {
return KVModule_ReplyWithError(ctx, KVMODULE_ERRORMSG_WRONGTYPE);
}
MemAllocObject *o;
if (type == KVMODULE_KEYTYPE_EMPTY) {
return KVModule_ReplyWithError(ctx, "ERR Memory has not been allocated");
} else {
o = KVModule_ModuleTypeGetValue(key);
}
if (o->mask & (1UL << block_index)) {
return KVModule_ReplyWithError(ctx, "ERR block is busy");
}
int ret = 0;
int nokey;
struct MemBlock *mem = (struct MemBlock *)KVModule_DictGet(mem_pool[KVModule_GetSelectedDb(ctx)], argv[1], &nokey);
if (!nokey && mem) {
size_t len;
const char *buf = KVModule_StringPtrLen(argv[3], &len);
ret = MemBlockWrite(mem, block_index, buf, len);
o->mask |= (1UL << block_index);
o->used++;
}
KVModule_ReplyWithLongLong(ctx, ret);
KVModule_ReplicateVerbatim(ctx);
return KVMODULE_OK;
}
/* MEM.READ key block_index */
int MemRead_RedisCommand(KVModuleCtx *ctx, KVModuleString **argv, int argc) {
KVModule_AutoMemory(ctx);
if (argc != 3) {
return KVModule_WrongArity(ctx);
}
long long block_index;
if ((KVModule_StringToLongLong(argv[2], &block_index) != KVMODULE_OK) || block_index < 0) {
return KVModule_ReplyWithError(ctx, "ERR invalid block_index: must be a value greater than 0");
}
KVModuleKey *key = KVModule_OpenKey(ctx, argv[1], KVMODULE_READ);
int type = KVModule_KeyType(key);
if (type != KVMODULE_KEYTYPE_EMPTY && KVModule_ModuleTypeGetType(key) != MemAllocType) {
return KVModule_ReplyWithError(ctx, KVMODULE_ERRORMSG_WRONGTYPE);
}
MemAllocObject *o;
if (type == KVMODULE_KEYTYPE_EMPTY) {
return KVModule_ReplyWithError(ctx, "ERR Memory has not been allocated");
} else {
o = KVModule_ModuleTypeGetValue(key);
}
if (!(o->mask & (1UL << block_index))) {
return KVModule_ReplyWithNull(ctx);
}
int nokey;
struct MemBlock *mem = (struct MemBlock *)KVModule_DictGet(mem_pool[KVModule_GetSelectedDb(ctx)], argv[1], &nokey);
KVModule_Assert(nokey == 0 && mem != NULL);
char buf[BLOCK_SIZE];
MemBlockRead(mem, block_index, buf, sizeof(buf));
/* Assuming that the contents are all c-style strings */
KVModule_ReplyWithStringBuffer(ctx, buf, strlen(buf));
return KVMODULE_OK;
}
/* MEM.USAGE dbid */
int MemUsage_RedisCommand(KVModuleCtx *ctx, KVModuleString **argv, int argc) {
KVModule_AutoMemory(ctx);
if (argc != 2) {
return KVModule_WrongArity(ctx);
}
long long dbid;
if ((KVModule_StringToLongLong(argv[1], (long long *)&dbid) != KVMODULE_OK)) {
return KVModule_ReplyWithError(ctx, "ERR invalid value: must be a integer");
}
if (dbid < 0 || dbid >= MAX_DB) {
return KVModule_ReplyWithError(ctx, "ERR dbid out of range");
}
long long size = 0, used = 0;
void *data;
KVModuleString *key;
KVModuleDictIter *iter = KVModule_DictIteratorStartC(mem_pool[dbid], "^", NULL, 0);
while((key = KVModule_DictNext(ctx, iter, &data)) != NULL) {
int dbbackup = KVModule_GetSelectedDb(ctx);
KVModule_SelectDb(ctx, dbid);
KVModuleKey *openkey = KVModule_OpenKey(ctx, key, KVMODULE_READ);
int type = KVModule_KeyType(openkey);
KVModule_Assert(type != KVMODULE_KEYTYPE_EMPTY && KVModule_ModuleTypeGetType(openkey) == MemAllocType);
MemAllocObject *o = KVModule_ModuleTypeGetValue(openkey);
used += o->used;
size += o->size;
KVModule_CloseKey(openkey);
KVModule_SelectDb(ctx, dbbackup);
}
KVModule_DictIteratorStop(iter);
KVModule_ReplyWithArray(ctx, 4);
KVModule_ReplyWithSimpleString(ctx, "total");
KVModule_ReplyWithLongLong(ctx, size);
KVModule_ReplyWithSimpleString(ctx, "used");
KVModule_ReplyWithLongLong(ctx, used);
return KVMODULE_OK;
}
/* MEM.ALLOCANDWRITE key block_num block_index data block_index data ... */
int MemAllocAndWrite_RedisCommand(KVModuleCtx *ctx, KVModuleString **argv, int argc) {
KVModule_AutoMemory(ctx);
if (argc < 3) {
return KVModule_WrongArity(ctx);
}
long long block_num;
if ((KVModule_StringToLongLong(argv[2], &block_num) != KVMODULE_OK) || block_num <= 0) {
return KVModule_ReplyWithError(ctx, "ERR invalid block_num: must be a value greater than 0");
}
KVModuleKey *key = KVModule_OpenKey(ctx, argv[1], KVMODULE_READ | KVMODULE_WRITE);
int type = KVModule_KeyType(key);
if (type != KVMODULE_KEYTYPE_EMPTY && KVModule_ModuleTypeGetType(key) != MemAllocType) {
return KVModule_ReplyWithError(ctx, KVMODULE_ERRORMSG_WRONGTYPE);
}
MemAllocObject *o;
if (type == KVMODULE_KEYTYPE_EMPTY) {
o = createMemAllocObject();
KVModule_ModuleTypeSetValue(key, MemAllocType, o);
} else {
o = KVModule_ModuleTypeGetValue(key);
}
struct MemBlock *mem = MemBlockCreate(block_num);
KVModule_Assert(mem != NULL);
KVModule_DictSet(mem_pool[KVModule_GetSelectedDb(ctx)], argv[1], mem);
o->used = 0;
o->mask = 0;
o->size = block_num;
int i = 3;
long long block_index;
for (; i < argc; i++) {
/* Security is guaranteed internally, so no security check. */
KVModule_StringToLongLong(argv[i], &block_index);
size_t len;
const char * buf = KVModule_StringPtrLen(argv[i + 1], &len);
MemBlockWrite(mem, block_index, buf, len);
o->used++;
o->mask |= (1UL << block_index);
}
KVModule_ReplyWithSimpleString(ctx, "OK");
KVModule_ReplicateVerbatim(ctx);
return KVMODULE_OK;
}
/*---------------------------- type callbacks ------------------------------------*/
void *MemAllocRdbLoad(KVModuleIO *rdb, int encver) {
if (encver != 0) {
return NULL;
}
MemAllocObject *o = createMemAllocObject();
o->size = KVModule_LoadSigned(rdb);
o->used = KVModule_LoadSigned(rdb);
o->mask = KVModule_LoadUnsigned(rdb);
const KVModuleString *key = KVModule_GetKeyNameFromIO(rdb);
int dbid = KVModule_GetDbIdFromIO(rdb);
if (o->size) {
size_t size;
char *tmpbuf;
long long num = o->size;
struct MemBlock *head = KVModule_Calloc(1, sizeof(struct MemBlock));
tmpbuf = KVModule_LoadStringBuffer(rdb, &size);
memcpy(head->block, tmpbuf, size > BLOCK_SIZE ? BLOCK_SIZE:size);
KVModule_Free(tmpbuf);
struct MemBlock *block = head;
while (--num) {
block->next = KVModule_Calloc(1, sizeof(struct MemBlock));
block = block->next;
tmpbuf = KVModule_LoadStringBuffer(rdb, &size);
memcpy(block->block, tmpbuf, size > BLOCK_SIZE ? BLOCK_SIZE:size);
KVModule_Free(tmpbuf);
}
KVModule_DictSet(mem_pool[dbid], (KVModuleString *)key, head);
}
return o;
}
void MemAllocRdbSave(KVModuleIO *rdb, void *value) {
MemAllocObject *o = value;
KVModule_SaveSigned(rdb, o->size);
KVModule_SaveSigned(rdb, o->used);
KVModule_SaveUnsigned(rdb, o->mask);
const KVModuleString *key = KVModule_GetKeyNameFromIO(rdb);
int dbid = KVModule_GetDbIdFromIO(rdb);
if (o->size) {
int nokey;
struct MemBlock *mem = (struct MemBlock *)KVModule_DictGet(mem_pool[dbid], (KVModuleString *)key, &nokey);
KVModule_Assert(nokey == 0 && mem != NULL);
struct MemBlock *block = mem;
while (block) {
KVModule_SaveStringBuffer(rdb, block->block, BLOCK_SIZE);
block = block->next;
}
}
}
void MemAllocAofRewrite(KVModuleIO *aof, KVModuleString *key, void *value) {
MemAllocObject *o = (MemAllocObject *)value;
if (o->size) {
int dbid = KVModule_GetDbIdFromIO(aof);
int nokey;
size_t i = 0, j = 0;
struct MemBlock *mem = (struct MemBlock *)KVModule_DictGet(mem_pool[dbid], (KVModuleString *)key, &nokey);
KVModule_Assert(nokey == 0 && mem != NULL);
size_t array_size = o->size * 2;
KVModuleString ** string_array = KVModule_Calloc(array_size, sizeof(KVModuleString *));
while (mem) {
string_array[i] = KVModule_CreateStringFromLongLong(NULL, j);
string_array[i + 1] = KVModule_CreateString(NULL, mem->block, BLOCK_SIZE);
mem = mem->next;
i += 2;
j++;
}
KVModule_EmitAOF(aof, "mem.allocandwrite", "slv", key, o->size, string_array, array_size);
for (i = 0; i < array_size; i++) {
KVModule_FreeString(NULL, string_array[i]);
}
KVModule_Free(string_array);
} else {
KVModule_EmitAOF(aof, "mem.allocandwrite", "sl", key, o->size);
}
}
void MemAllocFree(void *value) {
KVModule_Free(value);
}
void MemAllocUnlink(KVModuleString *key, const void *value) {
KVMODULE_NOT_USED(key);
KVMODULE_NOT_USED(value);
/* When unlink and unlink2 exist at the same time, we will only call unlink2. */
KVModule_Assert(0);
}
void MemAllocUnlink2(KVModuleKeyOptCtx *ctx, const void *value) {
MemAllocObject *o = (MemAllocObject *)value;
const KVModuleString *key = KVModule_GetKeyNameFromOptCtx(ctx);
int dbid = KVModule_GetDbIdFromOptCtx(ctx);
if (o->size) {
void *oldval;
KVModule_DictDel(mem_pool[dbid], (KVModuleString *)key, &oldval);
KVModule_Assert(oldval != NULL);
MemBlockFree((struct MemBlock *)oldval);
}
}
void MemAllocDigest(KVModuleDigest *md, void *value) {
MemAllocObject *o = (MemAllocObject *)value;
KVModule_DigestAddLongLong(md, o->size);
KVModule_DigestAddLongLong(md, o->used);
KVModule_DigestAddLongLong(md, o->mask);
int dbid = KVModule_GetDbIdFromDigest(md);
const KVModuleString *key = KVModule_GetKeyNameFromDigest(md);
if (o->size) {
int nokey;
struct MemBlock *mem = (struct MemBlock *)KVModule_DictGet(mem_pool[dbid], (KVModuleString *)key, &nokey);
KVModule_Assert(nokey == 0 && mem != NULL);
struct MemBlock *block = mem;
while (block) {
KVModule_DigestAddStringBuffer(md, (const char *)block->block, BLOCK_SIZE);
block = block->next;
}
}
}
void *MemAllocCopy2(KVModuleKeyOptCtx *ctx, const void *value) {
const MemAllocObject *old = value;
MemAllocObject *new = createMemAllocObject();
new->size = old->size;
new->used = old->used;
new->mask = old->mask;
int from_dbid = KVModule_GetDbIdFromOptCtx(ctx);
int to_dbid = KVModule_GetToDbIdFromOptCtx(ctx);
const KVModuleString *fromkey = KVModule_GetKeyNameFromOptCtx(ctx);
const KVModuleString *tokey = KVModule_GetToKeyNameFromOptCtx(ctx);
if (old->size) {
int nokey;
struct MemBlock *oldmem = (struct MemBlock *)KVModule_DictGet(mem_pool[from_dbid], (KVModuleString *)fromkey, &nokey);
KVModule_Assert(nokey == 0 && oldmem != NULL);
struct MemBlock *newmem = MemBlockClone(oldmem);
KVModule_Assert(newmem != NULL);
KVModule_DictSet(mem_pool[to_dbid], (KVModuleString *)tokey, newmem);
}
return new;
}
size_t MemAllocMemUsage2(KVModuleKeyOptCtx *ctx, const void *value, size_t sample_size) {
KVMODULE_NOT_USED(ctx);
KVMODULE_NOT_USED(sample_size);
uint64_t size = 0;
MemAllocObject *o = (MemAllocObject *)value;
size += sizeof(*o);
size += o->size * sizeof(struct MemBlock);
return size;
}
size_t MemAllocMemFreeEffort2(KVModuleKeyOptCtx *ctx, const void *value) {
KVMODULE_NOT_USED(ctx);
MemAllocObject *o = (MemAllocObject *)value;
return o->size;
}
int KVModule_OnLoad(KVModuleCtx *ctx, KVModuleString **argv, int argc) {
KVMODULE_NOT_USED(argv);
KVMODULE_NOT_USED(argc);
if (KVModule_Init(ctx, "datatype2", 1,KVMODULE_APIVER_1) == KVMODULE_ERR) {
return KVMODULE_ERR;
}
KVModuleTypeMethods tm = {
.version = KVMODULE_TYPE_METHOD_VERSION,
.rdb_load = MemAllocRdbLoad,
.rdb_save = MemAllocRdbSave,
.aof_rewrite = MemAllocAofRewrite,
.free = MemAllocFree,
.digest = MemAllocDigest,
.unlink = MemAllocUnlink,
// .defrag = MemAllocDefrag, // Tested in defragtest.c
.unlink2 = MemAllocUnlink2,
.copy2 = MemAllocCopy2,
.mem_usage2 = MemAllocMemUsage2,
.free_effort2 = MemAllocMemFreeEffort2,
};
MemAllocType = KVModule_CreateDataType(ctx, "mem_alloc", 0, &tm);
if (MemAllocType == NULL) {
return KVMODULE_ERR;
}
/* This option tests skip command validation for KVModule_EmitAOF */
KVModule_SetModuleOptions(ctx, KVMODULE_OPTIONS_SKIP_COMMAND_VALIDATION);
if (KVModule_CreateCommand(ctx, "mem.alloc", MemAlloc_RedisCommand, "write deny-oom", 1, 1, 1) == KVMODULE_ERR) {
return KVMODULE_ERR;
}
if (KVModule_CreateCommand(ctx, "mem.free", MemFree_RedisCommand, "write deny-oom", 1, 1, 1) == KVMODULE_ERR) {
return KVMODULE_ERR;
}
if (KVModule_CreateCommand(ctx, "mem.write", MemWrite_RedisCommand, "write deny-oom", 1, 1, 1) == KVMODULE_ERR) {
return KVMODULE_ERR;
}
if (KVModule_CreateCommand(ctx, "mem.read", MemRead_RedisCommand, "readonly", 1, 1, 1) == KVMODULE_ERR) {
return KVMODULE_ERR;
}
if (KVModule_CreateCommand(ctx, "mem.usage", MemUsage_RedisCommand, "readonly", 1, 1, 1) == KVMODULE_ERR) {
return KVMODULE_ERR;
}
/* used for internal aof rewrite */
if (KVModule_CreateCommand(ctx, "mem.allocandwrite", MemAllocAndWrite_RedisCommand, "write deny-oom", 1, 1, 1) == KVMODULE_ERR) {
return KVMODULE_ERR;
}
for(int i = 0; i < MAX_DB; i++){
mem_pool[i] = KVModule_CreateDict(NULL);
}
KVModule_SubscribeToServerEvent(ctx, KVModuleEvent_FlushDB, flushdbCallback);
KVModule_SubscribeToServerEvent(ctx, KVModuleEvent_SwapDB, swapDbCallback);
return KVMODULE_OK;
}
int KVModule_OnUnload(KVModuleCtx *ctx) {
KVMODULE_NOT_USED(ctx);
for(int i = 0; i < MAX_DB; i++){
KVModuleString *key;
void *tdata;
KVModuleDictIter *iter = KVModule_DictIteratorStartC(mem_pool[i], "^", NULL, 0);
while((key = KVModule_DictNext(ctx, iter, &tdata)) != NULL) {
MemBlockFree((struct MemBlock *)tdata);
KVModule_FreeString(ctx, key);
}
KVModule_DictIteratorStop(iter);
KVModule_FreeDict(NULL, mem_pool[i]);
}
return KVMODULE_OK;
}