summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_debug_chunk.c
blob: 36629198a0934416d6000872ea1dc4d000c488e1 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* EINA - EFL data type library
 * Copyright (C) 2015 Carsten Haitzler
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>

#include "eina_debug.h"

# ifdef HAVE_CONFIG_H
#  include "config.h"
# endif

#ifdef HAVE_MMAP
# include <sys/mman.h>

// custom memory allocators to avoid malloc/free during backtrace handling
// just in case we're inside some signal handler due to mem corruption and
// are inside a malloc/free lock and thus would deadlock ourselves if we
// allocated memory, so implement scratch space just big enough for what we
// need and then some via either a static 8k+4k buffer pair or via a growable
// mmaped mem chunk pair
// implement using mmap so we can grow if needed - unlikelt though
static unsigned char *chunk1 = NULL;
static unsigned char *chunk2 = NULL;
static unsigned char *chunk3 = NULL;
static int chunk1_size = 0;
static int chunk1_num = 0;
static int chunk2_size = 0;
static int chunk2_num = 0;
static int chunk3_size = 0;
static int chunk3_num = 0;

// get a new chunk of "anonymous mmaped memory"
static void *
_eina_debug_chunk_need(int size)
{
   void *ptr;

   ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
              MAP_PRIVATE | MAP_ANON, -1, 0);
   if (ptr == MAP_FAILED) return NULL;
   return ptr;
}

// release a chunk of this mmaped anon mem if we don't need it anymore
static void
_eina_debug_chunk_noneed(void *ptr, int size)
{
   munmap(ptr, size);
}

// push a new bit of mem on our growing stack of mem - given our workload,
// we never free anything here, only ever grow new things on this stack
void *
_eina_debug_chunk_push(int size)
{
   void *ptr;

   // no initial chunk1 block - allocate it
   if (!chunk1)
     {
        chunk1 = _eina_debug_chunk_need(8 * 1024);
        if (!chunk1) return NULL;
        chunk1_size = 8 * 1024;
     }
   // round size up to the nearest pointer size for alignment
   size = sizeof(void *) * ((size + sizeof(void *) - 1) / sizeof(void *));
   // if our chunk is too small - grow it
   if ((chunk1_num + size) > chunk1_size)
     {
        // get a new chunk twice as big
        void *newchunk = _eina_debug_chunk_need(chunk1_size * 2);
        if (!newchunk) return NULL;
        // copy content over
        memcpy(newchunk, chunk1, chunk1_num);
        // release old chunk
        _eina_debug_chunk_noneed(chunk1, chunk1_size);
        // switch to our new 2x as big chunk
        chunk1 = newchunk;
        chunk1_size = chunk1_size * 2;
     }
   // get the mem at the top of this stack and return it, then move along
   ptr = chunk1 + chunk1_num;
   chunk1_num += size;
   return ptr;
}

// grow a single existing chunk (we use this for the filename -> path lookup)
void *
_eina_debug_chunk_realloc(int size)
{
   // we have a null/empty second chunk - allocate one
   if (!chunk2)
     {
        chunk2 = _eina_debug_chunk_need(4 * 1024);
        if (!chunk2) return NULL;
        chunk2_size = 4 * 1024;
     }
   // if our chunk is too small - grow it
   if (size > chunk2_size)
     {
        // get a new chunk twice as big
        void *newchunk = _eina_debug_chunk_need(chunk2_size * 2);
        if (!newchunk) return NULL;
        // copy content over
        memcpy(newchunk, chunk2, chunk2_num);
        // release old chunk
        _eina_debug_chunk_noneed(chunk2, chunk2_size);
        // switch to our new 2x as big chunk
        chunk2 = newchunk;
        chunk2_size = chunk2_size * 2;
     }
   // record new size and return chunk ptr as we just re-use it
   chunk2_num = size;
   return chunk2;
}

// grow a single existing chunk (we use this for the filename -> path lookup)
void *
_eina_debug_chunk_tmp_push(int size)
{
   void *ptr;

   // no initial chunk1 block - allocate it
   if (!chunk3)
     {
        chunk3 = _eina_debug_chunk_need(32 * 1024);
        if (!chunk3) return NULL;
        chunk3_size = 32 * 1024;
     }
   // round size up to the nearest pointer size for alignment
   size = sizeof(void *) * ((size + sizeof(void *) - 1) / sizeof(void *));
   // if our chunk is too small - grow it
   if ((chunk3_num + size) > chunk3_size)
     {
        // get a new chunk twice as big
        void *newchunk = _eina_debug_chunk_need(chunk3_size * 2);
        if (!newchunk) return NULL;
        // copy content over
        memcpy(newchunk, chunk3, chunk3_num);
        // release old chunk
        _eina_debug_chunk_noneed(chunk3, chunk3_size);
        // switch to our new 2x as big chunk
        chunk3 = newchunk;
        chunk3_size = chunk3_size * 2;
     }
   // get the mem at the top of this stack and return it, then move along
   ptr = chunk3 + chunk3_num;
   chunk3_num += size;
   return ptr;
}

void
_eina_debug_chunk_tmp_reset(void)
{
   chunk3_num = 0;
}
# else
// implement with static buffers - once we exceed these we will fail. sorry
// maybe one day find another solution, but these buffers should be enough
// for now for thos eplatforms (like windows) where we can't do the mmap
// tricks above.
static unsigned char chunk1[8 * 1024];
static unsigned char chunk2[4 * 1024];
static unsigned char chunk3[128 * 1024];
static int chunk1_size = sizeof(chunk1);
static int chunk1_num = 0;
static int chunk2_size = sizeof(chunk2);
static int chunk2_num = 0;
static int chunk3_size = sizeof(chunk3);
static int chunk3_num = 0;

// push a new bit of mem on our growing stack of mem - given our workload,
// we never free anything here, only ever grow new things on this stack
void *
_eina_debug_chunk_push(int size)
{
   void *ptr;

   // round size up to the nearest pointer size for alignment
   size = sizeof(void *) * ((size + sizeof(void *) - 1) / sizeof(void *));
   // if we ran out of space - fail
   if ((chunk1_num + size) > chunk1_size) return NULL;
   // get the mem at the top of this stack and return it, then move along
   ptr = chunk1 + chunk1_num;
   chunk1_num += size;
   return ptr;
}

// grow a single existing chunk (we use this for the filename -> path lookup)
void *
_eina_debug_chunk_realloc(int size)
{
   // if we ran out of space - fail
   if (size > chunk2_size) return NULL;
   // record new size and return chunk ptr as we just re-use it
   chunk2_num = size;
   return chunk2;
}

// grow a single existing chunk (we use this for the filename -> path lookup)
void *
_eina_debug_chunk_tmp_push(int size)
{
   void *ptr;

   // round size up to the nearest pointer size for alignment
   size = sizeof(void *) * ((size + sizeof(void *) - 1) / sizeof(void *));
   // if we ran out of space - fail
   if ((chunk3_num + size) > chunk3_size) return NULL;
   // get the mem at the top of this stack and return it, then move along
   ptr = chunk3 + chunk1_num;
   chunk3_num += size;
   return ptr;
}

void
_eina_debug_chunk_tmp_reset(void)
{
   chunk3_num = 0;
}
# endif

// handy - duplicate a string on our growing stack - never expect to free it
char *
_eina_debug_chunk_strdup(const char *str)
{
   int len = strlen(str);
   char *s = _eina_debug_chunk_push(len + 1);
   if (!s) return NULL;
   strcpy(s, str);
   return s;
}