11 void mem_init(MG
* mem
) {
12 memset(mem
, 0, sizeof *mem
);
15 void mem_free(MG
* mem
) {
16 if(mem
->mem
) free(mem
->mem
);
20 /* returns 1 if realloc was necessary, -1 if no realloc was necessary, 0 when realloc failed */
21 int mem_grow_if_needed(MG
*mem
, size_t newsize
) {
22 if(newsize
> mem
->capa
) {
23 size_t nucap
= mem
->capa
* 2;
26 if(nucap
& (PAGE_SIZE
-1)) {
28 nucap
&= ~(PAGE_SIZE
-1);
31 void *nu
= realloc(mem
->mem
, nucap
);
40 int mem_write(MG
*mem
, size_t offset
, void* data
, size_t size
) {
42 size_t needed
= offset
+ size
;
43 if(needed
< offset
) return 0; /* overflow */
44 if((ret
= mem_grow_if_needed(mem
, needed
))) {
45 memcpy((char*) mem
->mem
+ offset
, data
, size
);
46 if(needed
> mem
->used
) mem
->used
= needed
;
51 int mem_append(MG
*mem
, void* data
, size_t size
) {
52 return mem_write(mem
, mem
->used
, data
, size
);
55 void* mem_getptr(MG
* mem
, size_t offset
, size_t byteswanted
) {
56 if(!mem
->mem
|| offset
+ byteswanted
> mem
->used
) return 0;
57 return (char*)mem
->mem
+ offset
;
60 void mem_set(MG
* mem
, void* data
, size_t used
, size_t allocated
) {
63 mem
->capa
= allocated
;
66 int mem_write_stream(MG
* mem
, FILE* out
) {
67 if(!mem
->mem
|| !mem
->used
) return 0;
68 int ret
= (fwrite(mem
->mem
, 1, mem
->used
, out
) == mem
->used
);
72 int mem_write_file(MG
* mem
, char* fn
) {
73 FILE *out
= fopen(fn
, "w");
75 int ret
= mem_write_stream(mem
, out
);