-
Notifications
You must be signed in to change notification settings - Fork 0
/
td_init_mem.c
60 lines (54 loc) · 1.28 KB
/
td_init_mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
** Copyright 2015 K.J. Hermans (kees.jan.hermans@gmail.com)
** This code is part of simpledbm, an API to a dbm on a finite resource.
** License: BSD
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "td_private.h"
static
int td_mem_read
(td_t* td, unsigned off, void* buf, unsigned siz, void* ioarg)
{
DISCARD_PARAMETER(ioarg);
memcpy(buf, ((char*)(td->resource.mem)) + off, siz);
return 0;
}
static
int td_mem_write
(td_t* td, unsigned off, const void* buf, unsigned siz, void* ioarg)
{
DISCARD_PARAMETER(ioarg);
memcpy(((char*)(td->resource.mem)) + off, buf, siz);
return 0;
}
/**
* \ingroup btree
*
* Initializes a btree to use a fixed piece of memory as resource.
*
* \param td Non-NULL pointer to an initialized btree structure.
* \param flags Bits from the TDFLG_* values.
* \param mem Start of the memory to use a resource.
* \param size Size of the usable memory.
*
* \returns Zero on success, or a TDERR_* value on error.
*/
int td_init_mem
(td_t* td, unsigned flags, void* mem, unsigned size)
{
if (flags & TDFLG_EXTEND) {
RETURNERR(TDERR_INIT);
}
CHECK(td_init(td));
td->resource.mem = mem;
td->header.size = size;
td->read = td_mem_read;
td->write = td_mem_write;
CHECK(td_init2(td, "tdi", 1, flags));
return 0;
}
#ifdef __cplusplus
}
#endif