1 /*
2  * No copyright claimed, Public Domain
3  */
4 
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <unistd.h>
11 
12 #include <arcan_math.h>
13 #include <arcan_general.h>
14 
15 #ifndef REALLOC_STEP
16 #define REALLOC_STEP 16
17 #endif
18 
arcan_alloc_mem(size_t nb,enum arcan_memtypes type,enum arcan_memhint hint,enum arcan_memalign align)19 void* arcan_alloc_mem(size_t nb,
20 	enum arcan_memtypes type, enum arcan_memhint hint, enum arcan_memalign align)
21 {
22 	void* buf = malloc(nb);
23 
24 	if (!buf && (hint & ARCAN_MEM_NONFATAL) == 0)
25 		arcan_fatal("arcan_alloc_mem(), out of memory.\n");
26 
27 	if (hint & ARCAN_MEM_BZERO)
28 		memset(buf, '\0', nb);
29 
30 	return buf;
31 }
32 
arcan_mem_tick()33 void arcan_mem_tick()
34 {
35 }
36 
arcan_mem_growarr(struct arcan_strarr * res)37 void arcan_mem_growarr(struct arcan_strarr* res)
38 {
39 /* _alloc functions lacks a grow at the moment,
40  * after that this should ofc. be replaced. */
41 	char** newbuf = arcan_alloc_mem(
42 		(res->limit + REALLOC_STEP) * sizeof(char*),
43 		ARCAN_MEM_STRINGBUF, ARCAN_MEM_BZERO, ARCAN_MEMALIGN_NATURAL
44 	);
45 
46 	memcpy(newbuf, res->data, res->limit * sizeof(char*));
47 	arcan_mem_free(res->data);
48 	res->data= newbuf;
49 	res->limit += REALLOC_STEP;
50 }
51 
arcan_mem_freearr(struct arcan_strarr * res)52 void arcan_mem_freearr(struct arcan_strarr* res)
53 {
54 	if (!res || !res->data)
55 		return;
56 
57 	char** cptr = res->data;
58 	while (cptr && *cptr)
59 		arcan_mem_free(*cptr++);
60 
61 	arcan_mem_free(res->data);
62 
63 	memset(res, '\0', sizeof(struct arcan_strarr));
64 }
65 
66 /*
67  * Allocate memory intended for read-only or
68  * exec use (JIT, ...)
69  */
arcan_alloc_fillmem(const void * data,size_t ds,enum arcan_memtypes type,enum arcan_memhint hint,enum arcan_memalign align)70 void* arcan_alloc_fillmem(const void* data,
71 	size_t ds,
72 	enum arcan_memtypes type,
73 	enum arcan_memhint hint,
74 	enum arcan_memalign align)
75 {
76 	void* buf = arcan_alloc_mem(ds, type, hint, align);
77 
78 	if (!buf)
79 		return NULL;
80 
81 	memcpy(buf, data, ds);
82 	return buf;
83 }
84 
arcan_mem_free(void * inptr)85 void arcan_mem_free(void* inptr)
86 {
87 	free(inptr);
88 }
89 
90