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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
// implementation of generic tools
#include "tools.h"
#include <new>
//////////////////////////// pool ///////////////////////////
pool::pool()
{
blocks = 0;
allocnext(POOLSIZE);
for(int i = 0; i<MAXBUCKETS; i++) reuse[i] = NULL;
};
void *pool::alloc(size_t size)
{
if(size>MAXREUSESIZE)
{
return malloc(size);
}
else
{
size = bucket(size);
void **r = (void **)reuse[size];
if(r)
{
reuse[size] = *r;
return (void *)r;
}
else
{
size <<= PTRBITS;
if(left<size) allocnext(POOLSIZE);
char *r = p;
p += size;
left -= size;
return r;
};
};
};
void pool::dealloc(void *p, size_t size)
{
if(size>MAXREUSESIZE)
{
free(p);
}
else
{
size = bucket(size);
if(size) // only needed for 0-size free, are there any?
{
*((void **)p) = reuse[size];
reuse[size] = p;
};
};
};
void *pool::realloc(void *p, size_t oldsize, size_t newsize)
{
void *np = alloc(newsize);
if(!oldsize) return np;
memcpy(np, p, newsize>oldsize ? oldsize : newsize);
dealloc(p, oldsize);
return np;
};
void pool::dealloc_block(void *b)
{
if(b)
{
dealloc_block(*((char **)b));
free(b);
};
}
void pool::allocnext(size_t allocsize)
{
char *b = (char *)malloc(allocsize+PTRSIZE);
*((char **)b) = blocks;
blocks = b;
p = b+PTRSIZE;
left = allocsize;
};
char *pool::string(char *s, size_t l)
{
char *b = (char *)alloc(l+1);
strncpy(b,s,l);
b[l] = 0;
return b;
};
pool *gp() // useful for global buffers that need to be initialisation order independant
{
static pool *p = NULL;
return p ? p : (p = new pool());
};
///////////////////////// misc tools ///////////////////////
char *path(char *s)
{
for(char *t = s; t = strpbrk(t, "/\\"); *t++ = PATHDIV);
return s;
};
char *loadfile(char *fn, int *size)
{
FILE *f = fopen(fn, "rb");
if(!f) return NULL;
fseek(f, 0, SEEK_END);
int len = ftell(f);
fseek(f, 0, SEEK_SET);
char *buf = (char *)malloc(len+1);
if(!buf) return NULL;
buf[len] = 0;
size_t rlen = fread(buf, 1, len, f);
fclose(f);
if(len!=rlen || len<=0)
{
free(buf);
return NULL;
};
if(size!=NULL) *size = len;
return buf;
};
void endianswap(void *memory, int stride, int length) // little indians as storage format
{
if(*((char *)&stride)) return;
loop(w, length) loop(i, stride/2)
{
uchar *p = (uchar *)memory+w*stride;
uchar t = p[i];
p[i] = p[stride-i-1];
p[stride-i-1] = t;
};
}
|
|
|
>
|
>
|
|
<
|
<
|
<
|
|
|
<
|
|
<
|
<
|
>
|
|
|
|
|
|
|
>
|
|
<
|
<
|
<
|
|
|
|
|
|
|
>
|
|
>
|
|
|
|
>
|
|
<
|
|
|
>
|
|
|
|
|
|
>
|
|
|
|
|
>
|
>
|
|
<
>
|
|
>
|
>
|
|
>
|
|
|
|
|
>
|
|
|
|
|
<
|
|
|
>
|
|
>
>
|
|
>
|
|
|
|
|
|
|
|
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
// implementation of generic tools
#include "tools.h"
#include <new>
//////////////////////////// pool ///////////////////////////
pool::pool()
{
blocks = 0;
allocnext(POOLSIZE);
for (int i = 0; i < MAXBUCKETS; i++)
reuse[i] = NULL;
};
void *
pool::alloc(size_t size)
{
if (size > MAXREUSESIZE) {
return malloc(size);
} else {
size = bucket(size);
void **r = (void **)reuse[size];
if (r) {
reuse[size] = *r;
return (void *)r;
} else {
size <<= PTRBITS;
if (left < size)
allocnext(POOLSIZE);
char *r = p;
p += size;
left -= size;
return r;
};
};
};
void
pool::dealloc(void *p, size_t size)
{
if (size > MAXREUSESIZE) {
free(p);
} else {
size = bucket(size);
if (size) // only needed for 0-size free, are there any?
{
*((void **)p) = reuse[size];
reuse[size] = p;
};
};
};
void *
pool::realloc(void *p, size_t oldsize, size_t newsize)
{
void *np = alloc(newsize);
if (!oldsize)
return np;
memcpy(np, p, newsize > oldsize ? oldsize : newsize);
dealloc(p, oldsize);
return np;
};
void
pool::dealloc_block(void *b)
{
if (b) {
dealloc_block(*((char **)b));
free(b);
};
}
void
pool::allocnext(size_t allocsize)
{
char *b = (char *)malloc(allocsize + PTRSIZE);
*((char **)b) = blocks;
blocks = b;
p = b + PTRSIZE;
left = allocsize;
};
char *
pool::string(char *s, size_t l)
{
char *b = (char *)alloc(l + 1);
strncpy(b, s, l);
b[l] = 0;
return b;
};
pool *
gp() // useful for global buffers that need to be initialisation order
// independant
{
static pool *p = NULL;
return p ? p : (p = new pool());
};
///////////////////////// misc tools ///////////////////////
char *
path(char *s)
{
for (char *t = s; t = strpbrk(t, "/\\"); *t++ = PATHDIV)
;
return s;
};
char *
loadfile(char *fn, int *size)
{
FILE *f = fopen(fn, "rb");
if (!f)
return NULL;
fseek(f, 0, SEEK_END);
int len = ftell(f);
fseek(f, 0, SEEK_SET);
char *buf = (char *)malloc(len + 1);
if (!buf)
return NULL;
buf[len] = 0;
size_t rlen = fread(buf, 1, len, f);
fclose(f);
if (len != rlen || len <= 0) {
free(buf);
return NULL;
};
if (size != NULL)
*size = len;
return buf;
};
void
endianswap(
void *memory, int stride, int length) // little indians as storage format
{
if (*((char *)&stride))
return;
loop(w, length) loop(i, stride / 2)
{
uchar *p = (uchar *)memory + w * stride;
uchar t = p[i];
p[i] = p[stride - i - 1];
p[stride - i - 1] = t;
};
}
|