~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/pbxt/src/memory_xt.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2005 PrimeBase Technologies GmbH
 
2
 *
 
3
 * PrimeBase XT
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * 2005-01-04   Paul McCullagh
 
20
 *
 
21
 * H&G2JCtL
 
22
 */
 
23
 
 
24
#include "xt_config.h"
 
25
 
 
26
#include <stdio.h>
 
27
#include <errno.h>
 
28
#include <stdlib.h>
 
29
#include <string.h>
 
30
 
 
31
#include "pthread_xt.h"
 
32
#include "thread_xt.h"
 
33
#include "strutil_xt.h"
 
34
#include "trace_xt.h"
 
35
 
 
36
#ifdef DEBUG
 
37
//#define RECORD_MM
 
38
#endif
 
39
 
 
40
#ifdef DEBUG
 
41
 
 
42
#undef  xt_malloc
 
43
#undef  xt_calloc
 
44
#undef  xt_realloc
 
45
#undef  xt_free
 
46
#undef  xt_pfree
 
47
 
 
48
#undef  xt_malloc_ns
 
49
#undef  xt_calloc_ns
 
50
#undef  xt_realloc_ns
 
51
#undef  xt_free_ns
 
52
 
 
53
void    *xt_malloc(XTThreadPtr self, size_t size);
 
54
void    *xt_calloc(XTThreadPtr self, size_t size);
 
55
xtBool  xt_realloc(XTThreadPtr self, void **ptr, size_t size);
 
56
void    xt_free(XTThreadPtr self, void *ptr);
 
57
void    xt_pfree(XTThreadPtr self, void **ptr);
 
58
 
 
59
void    *xt_malloc_ns(size_t size);
 
60
void    *xt_calloc_ns(size_t size);
 
61
xtBool  xt_realloc_ns(void **ptr, size_t size);
 
62
void    xt_free_ns(void *ptr);
 
63
 
 
64
#define ADD_TOTAL_ALLOCS                        4000
 
65
 
 
66
#define SHIFT_RIGHT(ptr, n)                     memmove(((char *) (ptr)) + sizeof(MissingMemoryRec), (ptr), (long) (n) * sizeof(MissingMemoryRec))
 
67
#define SHIFT_LEFT(ptr, n)                      memmove((ptr), ((char *) (ptr)) + sizeof(MissingMemoryRec), (long) (n) * sizeof(MissingMemoryRec))
 
68
 
 
69
#define STACK_TRACE_DEPTH                       4
 
70
 
 
71
typedef struct MissingMemory {
 
72
        void                    *mm_ptr;
 
73
        xtWord4                 id;
 
74
        xtWord2                 line_nr;
 
75
        xtWord2                 trace_count;
 
76
        c_char                  *mm_file;
 
77
        c_char                  *mm_func[STACK_TRACE_DEPTH];
 
78
} MissingMemoryRec, *MissingMemoryPtr;
 
79
 
 
80
static MissingMemoryRec *mm_addresses = NULL;
 
81
static long                             mm_nr_in_use = 0L;
 
82
static long                             mm_total_allocated = 0L;
 
83
static xtWord4                  mm_alloc_count = 0;
 
84
static xt_mutex_type    mm_mutex;
 
85
 
 
86
#ifdef RECORD_MM
 
87
static long mm_find_pointer(void *ptr);
 
88
#endif
 
89
 
 
90
#endif
 
91
 
 
92
/*
 
93
 * -----------------------------------------------------------------------
 
94
 * STANDARD SYSTEM BASED MEMORY ALLOCATION
 
95
 */
 
96
 
 
97
xtPublic void *xt_malloc(XTThreadPtr self, size_t size)
 
98
{
 
99
        void *ptr;
 
100
 
 
101
        if (!(ptr = malloc(size))) {
 
102
                xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
 
103
                return NULL;
 
104
        }
 
105
        return ptr;
 
106
}
 
107
 
 
108
xtPublic xtBool xt_realloc(XTThreadPtr self, void **ptr, size_t size)
 
109
{
 
110
        void *new_ptr;
 
111
 
 
112
        if (!(new_ptr = realloc(*ptr, size))) {
 
113
                xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
 
114
                return FAILED;
 
115
        }
 
116
        *ptr = new_ptr;
 
117
        return OK;
 
118
}
 
119
 
 
120
xtPublic void xt_free(XTThreadPtr XT_UNUSED(self), void *ptr)
 
121
{
 
122
        free(ptr);
 
123
}
 
124
 
 
125
xtPublic void *xt_calloc(XTThreadPtr self, size_t size)
 
126
{
 
127
        void *ptr;
 
128
 
 
129
        if ((ptr = xt_malloc(self, size)))
 
130
                memset(ptr, 0, size);
 
131
        return ptr;
 
132
}
 
133
 
 
134
#undef  xt_pfree
 
135
 
 
136
xtPublic void xt_pfree(XTThreadPtr self, void **ptr)
 
137
{
 
138
        if (*ptr) {
 
139
                void *p = *ptr;
 
140
 
 
141
                *ptr = NULL;
 
142
                xt_free(self, p);
 
143
        }
 
144
}
 
145
 
 
146
/*
 
147
 * -----------------------------------------------------------------------
 
148
 * SYSTEM MEMORY ALLOCATION WITH A THREAD
 
149
 */
 
150
 
 
151
xtPublic void *xt_malloc_ns(size_t size)
 
152
{
 
153
        void *ptr;
 
154
 
 
155
        if (!(ptr = malloc(size))) {
 
156
                xt_register_errno(XT_REG_CONTEXT, XT_ENOMEM);
 
157
                return NULL;
 
158
        }
 
159
        return ptr;
 
160
}
 
161
 
 
162
xtPublic void *xt_calloc_ns(size_t size)
 
163
{
 
164
        void *ptr;
 
165
 
 
166
        if (!(ptr = malloc(size))) {
 
167
                xt_register_errno(XT_REG_CONTEXT, XT_ENOMEM);
 
168
                return NULL;
 
169
        }
 
170
        memset(ptr, 0, size);
 
171
        return ptr;
 
172
}
 
173
 
 
174
xtPublic xtBool xt_realloc_ns(void **ptr, size_t size)
 
175
{
 
176
        void *new_ptr;
 
177
 
 
178
        if (!(new_ptr = realloc(*ptr, size)))
 
179
                return xt_register_errno(XT_REG_CONTEXT, XT_ENOMEM);
 
180
        *ptr = new_ptr;
 
181
        return OK;
 
182
}
 
183
 
 
184
xtPublic void xt_free_ns(void *ptr)
 
185
{
 
186
        free(ptr);
 
187
}
 
188
 
 
189
#ifdef DEBUG_MEMORY
 
190
 
 
191
/*
 
192
 * -----------------------------------------------------------------------
 
193
 * MEMORY SEARCHING CODE
 
194
 */
 
195
 
 
196
#define MM_THROW_ASSERTION(str) mm_throw_assertion(self, __FUNC__, __FILE__, __LINE__, str)
 
197
 
 
198
static void mm_throw_assertion(XTThreadPtr self, c_char *func, c_char *file, u_int line, c_char *str)
 
199
{
 
200
        printf("***** MM:FATAL %s\n", str);
 
201
        xt_throw_assertion(self, func, file, line, str);
 
202
}
 
203
 
 
204
/*
 
205
 * -----------------------------------------------------------------------
 
206
 * MEMORY SEARCHING CODE
 
207
 */
 
208
 
 
209
static int mm_debug_ik_inc;
 
210
static int mm_debug_ik_dec;
 
211
static int mm_debug_ik_no;
 
212
 
 
213
xtPublic void mm_trace_print_id(void *ptr)
 
214
{
 
215
        (void) ptr;
 
216
#ifdef RECORD_MM
 
217
        long mm;
 
218
 
 
219
        xt_lock_mutex_ns(&mm_mutex);
 
220
 
 
221
        mm = mm_find_pointer(ptr);
 
222
        if (mm >= 0) {
 
223
                MissingMemoryPtr mm_ptr;
 
224
 
 
225
                mm_ptr = &mm_addresses[mm];
 
226
                printf("MM: %08lX (#%ld) %s:%d\n",
 
227
                                           (unsigned long) mm_ptr->mm_ptr,
 
228
                                           (long) mm_ptr->id,
 
229
                                           xt_last_name_of_path(mm_ptr->mm_file),
 
230
                                           (int) mm_ptr->line_nr);
 
231
        }
 
232
        xt_unlock_mutex_ns(&mm_mutex);
 
233
#endif
 
234
}
 
235
 
 
236
/*
 
237
 * Call this function where the missing memory
 
238
 * is referenced.
 
239
 */
 
240
xtPublic void mm_trace_inc(XTThreadPtr self, XTMMTraceRefPtr tr)
 
241
{
 
242
        int i;
 
243
 
 
244
#ifdef RECORD_MM
 
245
        if (xt_lock_mutex(self, &mm_mutex)) {
 
246
                long mm;
 
247
 
 
248
                mm = mm_find_pointer(tr);
 
249
                if (mm >= 0)
 
250
                        mm_addresses[mm].trace_count = 1;
 
251
                xt_unlock_mutex(self, &mm_mutex);
 
252
        }
 
253
#endif
 
254
        mm_debug_ik_inc++;
 
255
        if (tr->mm_pos < XT_MM_STACK_TRACE-1) {
 
256
                tr->mm_trace[tr->mm_pos++] = self->t_name[0] == 'S' ? XT_MM_TRACE_SW_INC : XT_MM_TRACE_INC;
 
257
                for (i=1; i<=XT_MM_TRACE_DEPTH; i++) {
 
258
                        if (self->t_call_top-i < 0)
 
259
                                break;
 
260
                        if (tr->mm_pos < XT_MM_STACK_TRACE-1) {
 
261
                                tr->mm_line[tr->mm_pos] = self->t_call_stack[self->t_call_top-i].cs_line;
 
262
                                tr->mm_trace[tr->mm_pos++] = self->t_call_stack[self->t_call_top-i].cs_func;
 
263
                        }
 
264
                        else if (tr->mm_pos < XT_MM_STACK_TRACE)
 
265
                                tr->mm_trace[tr->mm_pos++] = XT_MM_TRACE_ERROR;
 
266
                }
 
267
        }
 
268
        else if (tr->mm_pos < XT_MM_STACK_TRACE)
 
269
                tr->mm_trace[tr->mm_pos++] = XT_MM_TRACE_ERROR;
 
270
}
 
271
 
 
272
xtPublic void mm_trace_dec(XTThreadPtr self, XTMMTraceRefPtr tr)
 
273
{
 
274
        int i;
 
275
 
 
276
#ifdef RECORD_MM
 
277
        if (xt_lock_mutex(self, &mm_mutex)) {
 
278
                long mm;
 
279
 
 
280
                mm = mm_find_pointer(tr);
 
281
                if (mm >= 0)
 
282
                        mm_addresses[mm].trace_count = 1;
 
283
                xt_unlock_mutex(self, &mm_mutex);
 
284
        }
 
285
#endif
 
286
        mm_debug_ik_dec++;
 
287
        if (tr->mm_pos < XT_MM_STACK_TRACE-1) {
 
288
                tr->mm_trace[tr->mm_pos++] = self->t_name[0] == 'S' ? XT_MM_TRACE_SW_DEC : XT_MM_TRACE_DEC;
 
289
                for (i=1; i<=XT_MM_TRACE_DEPTH; i++) {
 
290
                        if (self->t_call_top-i < 0)
 
291
                                break;
 
292
                        if (tr->mm_pos < XT_MM_STACK_TRACE-1) {
 
293
                                tr->mm_line[tr->mm_pos] = self->t_call_stack[self->t_call_top-i].cs_line;
 
294
                                tr->mm_trace[tr->mm_pos++] = self->t_call_stack[self->t_call_top-i].cs_func;
 
295
                        }
 
296
                        else if (tr->mm_pos < XT_MM_STACK_TRACE)
 
297
                                tr->mm_trace[tr->mm_pos++] = XT_MM_TRACE_ERROR;
 
298
                }
 
299
        }
 
300
        else if (tr->mm_pos < XT_MM_STACK_TRACE)
 
301
                tr->mm_trace[tr->mm_pos++] = XT_MM_TRACE_ERROR;
 
302
}
 
303
 
 
304
xtPublic void mm_trace_init(XTThreadPtr self, XTMMTraceRefPtr tr)
 
305
{
 
306
        mm_debug_ik_no++;
 
307
        tr->mm_id = (u_int) mm_debug_ik_no;
 
308
        tr->mm_pos = 0;
 
309
        mm_trace_inc(self, tr);
 
310
}
 
311
 
 
312
xtPublic void mm_trace_print(XTMMTraceRefPtr tr)
 
313
{
 
314
        int i, cnt = 0;
 
315
 
 
316
        for (i=0; i<tr->mm_pos; i++) {
 
317
                if (tr->mm_trace[i] == XT_MM_TRACE_INC) {
 
318
                        if (i > 0)
 
319
                                printf("\n");
 
320
                        cnt++;
 
321
                        printf("INC (%d) ", cnt);
 
322
                }
 
323
                else if (tr->mm_trace[i] == XT_MM_TRACE_SW_INC) {
 
324
                        if (i > 0)
 
325
                                printf("\n");
 
326
                        printf("SW-DEC (%d) ", cnt);
 
327
                        cnt--;
 
328
                }
 
329
                else if (tr->mm_trace[i] == XT_MM_TRACE_DEC) {
 
330
                        if (i > 0)
 
331
                                printf("\n");
 
332
                        printf("DEC (%d) ", cnt);
 
333
                        cnt--;
 
334
                }
 
335
                else if (tr->mm_trace[i] == XT_MM_TRACE_SW_DEC) {
 
336
                        if (i > 0)
 
337
                                printf("\n");
 
338
                        printf("SW-DEC (%d) ", cnt);
 
339
                        cnt--;
 
340
                }
 
341
                else if (tr->mm_trace[i] == XT_MM_TRACE_ERROR) {
 
342
                        if (i > 0)
 
343
                                printf("\n");
 
344
                        printf("ERROR: Space out");
 
345
                }
 
346
                else
 
347
                        printf("%s(%d) ", tr->mm_trace[i], (int) tr->mm_line[i]);
 
348
        }
 
349
        printf("\n");
 
350
}
 
351
 
 
352
/* Call this function on exit, when you know the memory is missing. */
 
353
static void mm_debug_trace_count(XTMMTraceRefPtr tr)
 
354
{
 
355
        printf("MM Trace ID: %d\n", tr->mm_id);
 
356
        mm_trace_print(tr);
 
357
}
 
358
 
 
359
/* The give the sum of allocations, etc. */
 
360
static void mm_debug_trace_sum(void)
 
361
{
 
362
        if (mm_debug_ik_no) {
 
363
                printf("MM Trace INC: %d\n", mm_debug_ik_inc);
 
364
                printf("MM Trace DEC: %d\n", mm_debug_ik_dec);
 
365
                printf("MM Trace ALL: %d\n", mm_debug_ik_no);
 
366
        }
 
367
}
 
368
 
 
369
/*
 
370
 * -----------------------------------------------------------------------
 
371
 * DEBUG MEMORY ALLOCATION AND HEAP CHECKING
 
372
 */
 
373
 
 
374
#ifdef RECORD_MM
 
375
static long mm_find_pointer(void *ptr)
 
376
{
 
377
        register long   i, n, guess;
 
378
 
 
379
        i = 0;
 
380
        n = mm_nr_in_use;
 
381
        while (i < n) {
 
382
                guess = (i + n - 1) >> 1;
 
383
                if (ptr == mm_addresses[guess].mm_ptr)
 
384
                        return(guess);
 
385
                if (ptr < mm_addresses[guess].mm_ptr)
 
386
                        n = guess;
 
387
                else
 
388
                        i = guess + 1;
 
389
        }
 
390
        return(-1);
 
391
}
 
392
 
 
393
static long mm_add_pointer(void *ptr, u_int XT_UNUSED(id))
 
394
{
 
395
        register int    i, n, guess;
 
396
 
 
397
        if (mm_nr_in_use == mm_total_allocated) {
 
398
                /* Not enough space, add more: */
 
399
                MissingMemoryRec *new_addresses;
 
400
 
 
401
                new_addresses = (MissingMemoryRec *) xt_calloc_ns(sizeof(MissingMemoryRec) * (mm_total_allocated + ADD_TOTAL_ALLOCS));
 
402
                if (!new_addresses)
 
403
                        return(-1);
 
404
 
 
405
                if (mm_addresses) {
 
406
                        memcpy(new_addresses, mm_addresses, sizeof(MissingMemoryRec) * mm_total_allocated);
 
407
                        free(mm_addresses);
 
408
                }
 
409
 
 
410
                mm_addresses = new_addresses;
 
411
                mm_total_allocated += ADD_TOTAL_ALLOCS;
 
412
        }
 
413
 
 
414
        i = 0;
 
415
        n = mm_nr_in_use;
 
416
        while (i < n) {
 
417
                guess = (i + n - 1) >> 1;
 
418
                if (ptr < mm_addresses[guess].mm_ptr)
 
419
                        n = guess;
 
420
                else
 
421
                        i = guess + 1;
 
422
        }
 
423
 
 
424
        SHIFT_RIGHT(&mm_addresses[i], mm_nr_in_use - i);
 
425
        mm_nr_in_use++;
 
426
        mm_addresses[i].mm_ptr = ptr;
 
427
        return(i);
 
428
}
 
429
 
 
430
xtPublic char *mm_watch_point = 0;
 
431
 
 
432
static long mm_remove_pointer(void *ptr)
 
433
{
 
434
        register int    i, n, guess;
 
435
 
 
436
        if (mm_watch_point == ptr)
 
437
                printf("Hit watch point!\n");
 
438
 
 
439
        i = 0;
 
440
        n = mm_nr_in_use;
 
441
        while (i < n) {
 
442
                guess = (i + n - 1) >> 1;
 
443
                if (ptr == mm_addresses[guess].mm_ptr)
 
444
                        goto remove;
 
445
                if (ptr < mm_addresses[guess].mm_ptr)
 
446
                        n = guess;
 
447
                else
 
448
                        i = guess + 1;
 
449
        }
 
450
        return(-1);
 
451
 
 
452
        remove:
 
453
        /* Decrease the number of sets, and shift left: */
 
454
        mm_nr_in_use--;
 
455
        SHIFT_LEFT(&mm_addresses[guess], mm_nr_in_use - guess); 
 
456
        return(guess);
 
457
}
 
458
 
 
459
static void mm_add_core_ptr(XTThreadPtr self, void *ptr, u_int id, u_int line, c_char *file_name)
 
460
{
 
461
        long mm;
 
462
 
 
463
        mm = mm_add_pointer(ptr, id);
 
464
        if (mm < 0) {
 
465
                MM_THROW_ASSERTION("MM ERROR: Cannot allocate table big enough!");
 
466
                return;
 
467
        }
 
468
 
 
469
        /* Record the pointer: */
 
470
        if (mm_alloc_count >= 297 && mm_alloc_count <= 340) {
 
471
                if (id)
 
472
                        mm_addresses[mm].id = id;
 
473
                else
 
474
                        mm_addresses[mm].id = mm_alloc_count++;
 
475
        }
 
476
        else {
 
477
                if (id)
 
478
                        mm_addresses[mm].id = id;
 
479
                else
 
480
                        mm_addresses[mm].id = mm_alloc_count++;
 
481
        }
 
482
        mm_addresses[mm].mm_ptr = ptr;
 
483
        mm_addresses[mm].line_nr = (ushort) line;
 
484
        if (file_name)
 
485
                mm_addresses[mm].mm_file = file_name;
 
486
        else
 
487
                mm_addresses[mm].mm_file = "?";
 
488
        if (self) {
 
489
                for (int i=1; i<=STACK_TRACE_DEPTH; i++) {
 
490
                        if (self->t_call_top-i >= 0)
 
491
                                mm_addresses[mm].mm_func[i-1] = self->t_call_stack[self->t_call_top-i].cs_func;
 
492
                        else
 
493
                                mm_addresses[mm].mm_func[i-1] = NULL;
 
494
                }
 
495
        }
 
496
        else {
 
497
                for (int i=0; i<STACK_TRACE_DEPTH; i++)
 
498
                        mm_addresses[mm].mm_func[i] = NULL;
 
499
        }
 
500
}
 
501
 
 
502
static void mm_remove_core_ptr(void *ptr)
 
503
{
 
504
        XTThreadPtr     self = NULL;
 
505
        long            mm;
 
506
 
 
507
        mm = mm_remove_pointer(ptr);
 
508
        if (mm < 0) {
 
509
                MM_THROW_ASSERTION("Pointer not allocated");
 
510
                return;
 
511
        }
 
512
}
 
513
 
 
514
static void mm_throw_assertion(MissingMemoryPtr mm_ptr, void *p, c_char *message);
 
515
 
 
516
static long mm_find_core_ptr(void *ptr)
 
517
{
 
518
        long mm;
 
519
 
 
520
        mm = mm_find_pointer(ptr);
 
521
        if (mm < 0)
 
522
                mm_throw_assertion(NULL, ptr, "Pointer not allocated");
 
523
        return(mm);
 
524
}
 
525
 
 
526
static void mm_replace_core_ptr(long i, void *ptr)
 
527
{
 
528
        XTThreadPtr                     self = NULL;
 
529
        MissingMemoryRec        tmp = mm_addresses[i];
 
530
        long                            mm;
 
531
 
 
532
        mm_remove_pointer(mm_addresses[i].mm_ptr);
 
533
        mm = mm_add_pointer(ptr, mm_addresses[i].id);
 
534
        if (mm < 0) {
 
535
                MM_THROW_ASSERTION("Cannot allocate table big enough!");
 
536
                return;
 
537
        }
 
538
        mm_addresses[mm] = tmp;
 
539
        mm_addresses[mm].mm_ptr = ptr;
 
540
}
 
541
#endif
 
542
 
 
543
static void mm_throw_assertion(MissingMemoryPtr mm_ptr, void *p, c_char *message)
 
544
{
 
545
        XTThreadPtr     self = NULL;
 
546
        char            str[200];
 
547
 
 
548
        if (mm_ptr) {
 
549
                sprintf(str, "MM: %08lX (#%ld) %s:%d %s",
 
550
                                           (unsigned long) mm_ptr->mm_ptr,
 
551
                                           (long) mm_ptr->id,
 
552
                                           xt_last_name_of_path(mm_ptr->mm_file),
 
553
                                           (int) mm_ptr->line_nr,
 
554
                                           message);
 
555
        }
 
556
        else
 
557
                sprintf(str, "MM: %08lX %s", (unsigned long) p, message);
 
558
        MM_THROW_ASSERTION(str);
 
559
}
 
560
 
 
561
/*
 
562
 * -----------------------------------------------------------------------
 
563
 * MISSING MEMORY PUBLIC ROUTINES
 
564
 */
 
565
 
 
566
#define MEM_DEBUG_HDR_SIZE              offsetof(MemoryDebugRec, data)
 
567
#define MEM_TRAILER_SIZE                2
 
568
#define MEM_HEADER                              0x01010101
 
569
#define MEM_FREED                               0x03030303
 
570
#define MEM_TRAILER_BYTE                0x02
 
571
#define MEM_FREED_BYTE                  0x03
 
572
 
 
573
typedef struct MemoryDebug {
 
574
        xtWord4         check;
 
575
        xtWord4         size;
 
576
        char            data[200];
 
577
} MemoryDebugRec, *MemoryDebugPtr;
 
578
 
 
579
static size_t mm_checkmem(XTThreadPtr self, MissingMemoryPtr mm_ptr, void *p, xtBool freeme)
 
580
{
 
581
        unsigned char   *ptr    = (unsigned char *) p - MEM_DEBUG_HDR_SIZE;
 
582
        MemoryDebugPtr  debug_ptr = (MemoryDebugPtr) ptr;
 
583
        size_t                  size    = debug_ptr->size;
 
584
        long                    a_value;  /* Added to simplfy debugging. */
 
585
 
 
586
        if (!ASSERT(p)) 
 
587
                return(0);
 
588
        if (!ASSERT(((long) p & 1L) == 0)) 
 
589
                return(0);
 
590
        a_value = MEM_FREED;
 
591
        if (debug_ptr->check == MEM_FREED) { 
 
592
                mm_throw_assertion(mm_ptr, p, "Pointer already freed 'debug_ptr->check != MEM_FREED'");
 
593
                return(0);
 
594
        }
 
595
        a_value = MEM_HEADER;
 
596
        if (debug_ptr->check != MEM_HEADER) {
 
597
                mm_throw_assertion(mm_ptr, p, "Header not valid 'debug_ptr->check != MEM_HEADER'");
 
598
                return(0);
 
599
        }
 
600
        a_value = MEM_TRAILER_BYTE;
 
601
        if (!(*((unsigned char *) ptr + size + MEM_DEBUG_HDR_SIZE) == MEM_TRAILER_BYTE &&
 
602
                        *((unsigned char *) ptr + size + MEM_DEBUG_HDR_SIZE + 1L) == MEM_TRAILER_BYTE)) { 
 
603
                mm_throw_assertion(mm_ptr, p, "Trailer overwritten");
 
604
                return(0);
 
605
        }
 
606
 
 
607
        if (freeme) {
 
608
                debug_ptr->check = MEM_FREED;
 
609
                *((unsigned char *) ptr + size + MEM_DEBUG_HDR_SIZE) = MEM_FREED_BYTE;
 
610
                *((unsigned char *) ptr + size + MEM_DEBUG_HDR_SIZE + 1L) = MEM_FREED_BYTE;
 
611
 
 
612
                memset(((unsigned char *) ptr) + MEM_DEBUG_HDR_SIZE, 0xF5, size);
 
613
                xt_free(self, ptr);
 
614
        }
 
615
 
 
616
        return size;
 
617
}
 
618
 
 
619
xtBool xt_mm_scan_core(void)
 
620
{
 
621
        long mm;
 
622
 
 
623
        if (!mm_addresses)
 
624
                return TRUE;
 
625
 
 
626
        if (!xt_lock_mutex(NULL, &mm_mutex))
 
627
                return TRUE;
 
628
 
 
629
        for (mm=0; mm<mm_nr_in_use; mm++)       {
 
630
                mm_checkmem(NULL, &mm_addresses[mm], mm_addresses[mm].mm_ptr, FALSE);
 
631
        }
 
632
        
 
633
        xt_unlock_mutex(NULL, &mm_mutex);
 
634
        return TRUE;
 
635
}
 
636
 
 
637
void xt_mm_memmove(void *block, void *dest, void *source, size_t size)
 
638
{
 
639
        if (block) {
 
640
                MemoryDebugPtr  debug_ptr = (MemoryDebugPtr) ((char *) block - MEM_DEBUG_HDR_SIZE);
 
641
 
 
642
#ifdef RECORD_MM
 
643
                if (xt_lock_mutex(NULL, &mm_mutex)) {
 
644
                        mm_find_core_ptr(block);
 
645
                        xt_unlock_mutex(NULL, &mm_mutex);
 
646
                }
 
647
#endif
 
648
                mm_checkmem(NULL, NULL, block, FALSE);
 
649
 
 
650
                if (dest < block || (char *) dest > (char *) block + debug_ptr->size)
 
651
                        mm_throw_assertion(NULL, block, "Destination not in block");
 
652
                if ((char *) dest + size > (char *) block + debug_ptr->size)
 
653
                        mm_throw_assertion(NULL, block, "Copy will overwrite memory");
 
654
        }
 
655
 
 
656
        memmove(dest, source, size);
 
657
}
 
658
 
 
659
void xt_mm_memcpy(void *block, void *dest, void *source, size_t size)
 
660
{
 
661
        if (block) {
 
662
                MemoryDebugPtr  debug_ptr = (MemoryDebugPtr) ((char *) block - MEM_DEBUG_HDR_SIZE);
 
663
 
 
664
#ifdef RECORD_MM
 
665
                if (xt_lock_mutex(NULL, &mm_mutex)) {
 
666
                        mm_find_core_ptr(block);
 
667
                        xt_unlock_mutex(NULL, &mm_mutex);
 
668
                }
 
669
#endif
 
670
                mm_checkmem(NULL, NULL, block, FALSE);
 
671
 
 
672
                if (dest < block || (char *) dest > (char *) block + debug_ptr->size)
 
673
                        mm_throw_assertion(NULL, block, "Destination not in block");
 
674
                if ((char *) dest + size > (char *) block + debug_ptr->size)
 
675
                        mm_throw_assertion(NULL, block, "Copy will overwrite memory");
 
676
        }
 
677
 
 
678
        memcpy(dest, source, size);
 
679
}
 
680
 
 
681
void xt_mm_memset(void *block, void *dest, int value, size_t size)
 
682
{
 
683
        if (block) {
 
684
                MemoryDebugPtr  debug_ptr = (MemoryDebugPtr) ((char *) block - MEM_DEBUG_HDR_SIZE);
 
685
 
 
686
#ifdef RECORD_MM
 
687
                if (xt_lock_mutex(NULL, &mm_mutex)) {
 
688
                        mm_find_core_ptr(block);
 
689
                        xt_unlock_mutex(NULL, &mm_mutex);
 
690
                }
 
691
#endif
 
692
                mm_checkmem(NULL, NULL, block, FALSE);
 
693
 
 
694
                if (dest < block || (char *) dest > (char *) block + debug_ptr->size)
 
695
                        mm_throw_assertion(NULL, block, "Destination not in block");
 
696
                if ((char *) dest + size > (char *) block + debug_ptr->size)
 
697
                        mm_throw_assertion(NULL, block, "Copy will overwrite memory");
 
698
        }
 
699
 
 
700
        memset(dest, value, size);
 
701
}
 
702
 
 
703
void *xt_mm_malloc(XTThreadPtr self, size_t size, u_int line, c_char *file)
 
704
{
 
705
        unsigned char *p;
 
706
 
 
707
        if (size > (600*1024*1024))
 
708
                mm_throw_assertion(NULL, NULL, "Very large block allocated - meaybe error");
 
709
        p = (unsigned char *) xt_malloc(self, size + MEM_DEBUG_HDR_SIZE + MEM_TRAILER_SIZE);
 
710
        if (!p)
 
711
                return NULL;
 
712
 
 
713
        memset(p, 0x55, size + MEM_DEBUG_HDR_SIZE + MEM_TRAILER_SIZE);
 
714
 
 
715
        ((MemoryDebugPtr) p)->check = MEM_HEADER;
 
716
        ((MemoryDebugPtr) p)->size  = size;
 
717
        *(p + size + MEM_DEBUG_HDR_SIZE) = MEM_TRAILER_BYTE;
 
718
        *(p + size + MEM_DEBUG_HDR_SIZE + 1L) = MEM_TRAILER_BYTE;
 
719
 
 
720
        (void) line;
 
721
        (void) file;
 
722
#ifdef RECORD_MM
 
723
        xt_lock_mutex(self, &mm_mutex);
 
724
        mm_add_core_ptr(self, p + MEM_DEBUG_HDR_SIZE, 0, line, file);
 
725
        xt_unlock_mutex(self, &mm_mutex);
 
726
#endif
 
727
 
 
728
        return p + MEM_DEBUG_HDR_SIZE;
 
729
}
 
730
 
 
731
void *xt_mm_calloc(XTThreadPtr self, size_t size, u_int line, c_char *file)
 
732
{
 
733
        unsigned char *p;
 
734
        
 
735
        if (size > (500*1024*1024))
 
736
                mm_throw_assertion(NULL, NULL, "Very large block allocated - meaybe error");
 
737
        p = (unsigned char *) xt_calloc(self, size + MEM_DEBUG_HDR_SIZE + MEM_TRAILER_SIZE);
 
738
        if (!p) 
 
739
                return NULL;
 
740
 
 
741
        ((MemoryDebugPtr) p)->check = MEM_HEADER;
 
742
        ((MemoryDebugPtr) p)->size  = size;
 
743
        *(p + size + MEM_DEBUG_HDR_SIZE) = MEM_TRAILER_BYTE;
 
744
        *(p + size + MEM_DEBUG_HDR_SIZE + 1L) = MEM_TRAILER_BYTE;
 
745
 
 
746
        (void) line;
 
747
        (void) file;
 
748
#ifdef RECORD_MM
 
749
        xt_lock_mutex(self, &mm_mutex);
 
750
        mm_add_core_ptr(self, p + MEM_DEBUG_HDR_SIZE, 0, line, file);
 
751
        xt_unlock_mutex(self, &mm_mutex);
 
752
#endif
 
753
 
 
754
        return p + MEM_DEBUG_HDR_SIZE;
 
755
}
 
756
 
 
757
xtBool xt_mm_sys_realloc(XTThreadPtr self, void **ptr, size_t newsize, u_int line, c_char *file)
 
758
{
 
759
        return xt_mm_realloc(self, ptr, newsize, line, file);
 
760
}
 
761
 
 
762
xtBool xt_mm_realloc(XTThreadPtr self, void **ptr, size_t newsize, u_int line, c_char *file)
 
763
{
 
764
        unsigned char   *oldptr = (unsigned char *) *ptr;
 
765
        size_t                  size;
 
766
#ifdef RECORD_MM
 
767
        long                    mm;
 
768
#endif
 
769
        unsigned char   *pnew;
 
770
 
 
771
        if (!oldptr) {
 
772
                *ptr = xt_mm_malloc(self, newsize, line, file);
 
773
                return *ptr ? TRUE : FALSE;
 
774
        }
 
775
 
 
776
#ifdef RECORD_MM
 
777
        xt_lock_mutex(self, &mm_mutex);
 
778
        if ((mm = mm_find_core_ptr(oldptr)) < 0) {
 
779
                xt_unlock_mutex(self, &mm_mutex);
 
780
                xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
 
781
                return FAILED;
 
782
        }
 
783
        xt_unlock_mutex(self, &mm_mutex);
 
784
#endif
 
785
 
 
786
        oldptr = oldptr - MEM_DEBUG_HDR_SIZE;
 
787
        size = ((MemoryDebugPtr) oldptr)->size;
 
788
 
 
789
        ASSERT(((MemoryDebugPtr) oldptr)->check == MEM_HEADER);
 
790
        ASSERT(*((unsigned char *) oldptr + size + MEM_DEBUG_HDR_SIZE) == MEM_TRAILER_BYTE && 
 
791
                        *((unsigned char *) oldptr + size + MEM_DEBUG_HDR_SIZE + 1L) == MEM_TRAILER_BYTE);
 
792
 
 
793
        /* Realloc allways moves! */
 
794
        pnew = (unsigned char *) xt_malloc(self, newsize + MEM_DEBUG_HDR_SIZE + MEM_TRAILER_SIZE);
 
795
        if (!pnew) {
 
796
                xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
 
797
                return FAILED;
 
798
        }
 
799
 
 
800
        if (newsize > size) {
 
801
                memcpy(((MemoryDebugPtr) pnew)->data, ((MemoryDebugPtr) oldptr)->data, size);
 
802
                memset(((MemoryDebugPtr) pnew)->data + size, 0x55, newsize - size);
 
803
        }
 
804
        else
 
805
                memcpy(((MemoryDebugPtr) pnew)->data, ((MemoryDebugPtr) oldptr)->data, newsize);
 
806
 
 
807
        ((MemoryDebugPtr) pnew)->check = MEM_HEADER;
 
808
        ((MemoryDebugPtr) pnew)->size = newsize;
 
809
        *(pnew + newsize + MEM_DEBUG_HDR_SIZE) = MEM_TRAILER_BYTE;
 
810
        *(pnew + newsize + MEM_DEBUG_HDR_SIZE + 1L)     = MEM_TRAILER_BYTE;
 
811
 
 
812
#ifdef RECORD_MM
 
813
        xt_lock_mutex(self, &mm_mutex);
 
814
        if ((mm = mm_find_core_ptr(oldptr + MEM_DEBUG_HDR_SIZE)) < 0) {
 
815
                xt_unlock_mutex(self, &mm_mutex);
 
816
                xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
 
817
                return FAILED;
 
818
        }
 
819
        mm_replace_core_ptr(mm, pnew + MEM_DEBUG_HDR_SIZE);
 
820
        xt_unlock_mutex(self, &mm_mutex);
 
821
#endif
 
822
 
 
823
        memset(oldptr, 0x55, size + MEM_DEBUG_HDR_SIZE + MEM_TRAILER_SIZE);
 
824
        xt_free(self, oldptr);
 
825
 
 
826
        *ptr = pnew + MEM_DEBUG_HDR_SIZE;
 
827
        return OK;
 
828
}
 
829
 
 
830
void xt_mm_free(XTThreadPtr self, void *ptr)
 
831
{
 
832
#ifdef RECORD_MM
 
833
        if (xt_lock_mutex(self, &mm_mutex)) {
 
834
                mm_remove_core_ptr(ptr);
 
835
                xt_unlock_mutex(self, &mm_mutex);
 
836
        }
 
837
#endif
 
838
        mm_checkmem(self, NULL, ptr, TRUE);
 
839
}
 
840
 
 
841
void xt_mm_pfree(XTThreadPtr self, void **ptr)
 
842
{
 
843
        if (*ptr) {
 
844
                void *p = *ptr;
 
845
 
 
846
                *ptr = NULL;
 
847
                xt_mm_free(self, p);
 
848
        }
 
849
}
 
850
 
 
851
size_t xt_mm_malloc_size(XTThreadPtr self, void *ptr)
 
852
{
 
853
        size_t size = 0;
 
854
 
 
855
#ifdef RECORD_MM
 
856
        if (xt_lock_mutex(self, &mm_mutex)) {
 
857
                mm_find_core_ptr(ptr);
 
858
                xt_unlock_mutex(self, &mm_mutex);
 
859
        }
 
860
#endif
 
861
        size = mm_checkmem(self, NULL, ptr, FALSE);
 
862
        return size;
 
863
}
 
864
 
 
865
void xt_mm_check_ptr(XTThreadPtr self, void *ptr)
 
866
{
 
867
        mm_checkmem(self, NULL, ptr, FALSE);
 
868
}
 
869
#endif
 
870
 
 
871
/*
 
872
 * -----------------------------------------------------------------------
 
873
 * INIT/EXIT MEMORY
 
874
 */
 
875
 
 
876
xtPublic xtBool xt_init_memory(void)
 
877
{
 
878
#ifdef DEBUG_MEMORY
 
879
        XTThreadPtr     self = NULL;
 
880
 
 
881
        if (!xt_init_mutex_with_autoname(NULL, &mm_mutex))
 
882
                return FALSE;
 
883
 
 
884
        mm_addresses = (MissingMemoryRec *) malloc(sizeof(MissingMemoryRec) * ADD_TOTAL_ALLOCS);
 
885
        if (!mm_addresses) {
 
886
                MM_THROW_ASSERTION("MM ERROR: Insuffient memory to allocate MM table");
 
887
                xt_free_mutex(&mm_mutex);
 
888
                return FALSE;
 
889
        }
 
890
 
 
891
        memset(mm_addresses, 0, sizeof(MissingMemoryRec) * ADD_TOTAL_ALLOCS);
 
892
        mm_total_allocated = ADD_TOTAL_ALLOCS;
 
893
        mm_nr_in_use = 0L;
 
894
        mm_alloc_count = 0L;
 
895
#endif
 
896
        return TRUE;
 
897
}
 
898
 
 
899
xtPublic void debug_ik_count(void *value);
 
900
xtPublic void debug_ik_sum(void);
 
901
 
 
902
xtPublic void xt_exit_memory(void)
 
903
{
 
904
#ifdef DEBUG_MEMORY
 
905
        long    mm;
 
906
        int             i;
 
907
 
 
908
        if (!mm_addresses)
 
909
                return;
 
910
 
 
911
        xt_lock_mutex(NULL, &mm_mutex);
 
912
        for (mm=0; mm<mm_nr_in_use; mm++) {
 
913
                MissingMemoryPtr mm_ptr = &mm_addresses[mm];
 
914
 
 
915
                xt_logf(XT_NS_CONTEXT, XT_LOG_FATAL, "MM: %p (#%ld) %s:%d Not freed\n",
 
916
                        mm_ptr->mm_ptr,
 
917
                        (long) mm_ptr->id,
 
918
                        xt_last_name_of_path(mm_ptr->mm_file),
 
919
                        (int) mm_ptr->line_nr);
 
920
                for (i=0; i<STACK_TRACE_DEPTH; i++) {
 
921
                        if (mm_ptr->mm_func[i])
 
922
                                xt_logf(XT_NS_CONTEXT, XT_LOG_FATAL, "MM: %s\n", mm_ptr->mm_func[i]);
 
923
                }
 
924
                /*
 
925
                 * Assumes we place out tracing function in the first
 
926
                 * position!!
 
927
                 */
 
928
                if (mm_ptr->trace_count)
 
929
                        mm_debug_trace_count((XTMMTraceRefPtr) mm_ptr->mm_ptr);
 
930
        }
 
931
        mm_debug_trace_sum();
 
932
        free(mm_addresses);
 
933
        mm_addresses = NULL;
 
934
        mm_nr_in_use = 0L;
 
935
        mm_total_allocated = 0L;
 
936
        mm_alloc_count = 0L;
 
937
        xt_unlock_mutex(NULL, &mm_mutex);
 
938
 
 
939
        xt_free_mutex(&mm_mutex);
 
940
#endif
 
941
}
 
942
 
 
943
/*
 
944
 * -----------------------------------------------------------------------
 
945
 * MEMORY ALLOCATION UTILITIES
 
946
 */
 
947
 
 
948
#ifdef DEBUG_MEMORY
 
949
char    *xt_mm_dup_string(XTThreadPtr self, c_char *str, u_int line, c_char *file)
 
950
#else
 
951
char    *xt_dup_string(XTThreadPtr self, c_char *str)
 
952
#endif
 
953
{
 
954
        size_t  len;
 
955
        char    *new_str;
 
956
 
 
957
        if (!str)
 
958
                return NULL;
 
959
        len = strlen(str);
 
960
#ifdef DEBUG_MEMORY
 
961
        new_str = (char *) xt_mm_malloc(self, len + 1, line, file);
 
962
#else
 
963
        new_str = (char *) xt_malloc(self, len + 1);
 
964
#endif
 
965
        if (new_str)
 
966
                strcpy(new_str, str);
 
967
        return new_str;
 
968
}
 
969
 
 
970
xtPublic char *xt_long_to_str(XTThreadPtr self, long v)
 
971
{
 
972
        char str[50];
 
973
 
 
974
        sprintf(str, "%lu", v);
 
975
        return xt_dup_string(self, str);
 
976
}
 
977
 
 
978
char *xt_dup_nstr(XTThreadPtr self, c_char *str, int start, size_t len)
 
979
{
 
980
        char *new_str = (char *) xt_malloc(self, len + 1);
 
981
        
 
982
        if (new_str) {
 
983
                memcpy(new_str, str + start, len);
 
984
                new_str[len] = 0;
 
985
        }
 
986
        return new_str;
 
987
}
 
988
 
 
989
/*
 
990
 * -----------------------------------------------------------------------
 
991
 * LIGHT WEIGHT CHECK FUNCTIONS
 
992
 * Timing related memory management problems my not like the memset
 
993
 * or other heavy checking. Try this...
 
994
 */
 
995
 
 
996
#ifdef LIGHT_WEIGHT_CHECKS
 
997
xtPublic void *xt_malloc(XTThreadPtr self, size_t size)
 
998
{
 
999
        char *ptr;
 
1000
 
 
1001
        if (!(ptr = (char *) malloc(size+8))) {
 
1002
                xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
 
1003
                return NULL;
 
1004
        }
 
1005
        *((xtWord4 *) ptr) = size;
 
1006
        *((xtWord4 *) (ptr + size + 4)) = 0x7E7EFEFE;
 
1007
        return ptr+4;
 
1008
}
 
1009
 
 
1010
xtPublic void xt_check_ptr(void *ptr)
 
1011
{
 
1012
        char *old_ptr;
 
1013
        xtWord4 size;
 
1014
 
 
1015
        old_ptr = (char *) ptr;
 
1016
        old_ptr -= 4;
 
1017
        size = *((xtWord4 *) old_ptr);
 
1018
        if (size == 0xDEADBEAF || *((xtWord4 *) (old_ptr + size + 4)) != 0x7E7EFEFE) {
 
1019
                char *dummy = NULL;
 
1020
                
 
1021
                xt_dump_trace();
 
1022
                *dummy = 40;
 
1023
        }
 
1024
}
 
1025
 
 
1026
xtPublic xtBool xt_realloc(XTThreadPtr self, void **ptr, size_t size)
 
1027
{
 
1028
        char *old_ptr;
 
1029
        char *new_ptr;
 
1030
 
 
1031
        if ((old_ptr = (char *) *ptr)) {
 
1032
                void check_for_file(char *my_ptr, xtWord4 len);
 
1033
 
 
1034
                xt_check_ptr(old_ptr);
 
1035
                check_for_file((char *) old_ptr, *((xtWord4 *) (old_ptr - 4)));
 
1036
                if (!(new_ptr = (char *) realloc(old_ptr - 4, size+8))) {
 
1037
                        xt_throw_errno(XT_CONTEXT, XT_ENOMEM);
 
1038
                        return FAILED;
 
1039
                }
 
1040
                *((xtWord4 *) new_ptr) = size;
 
1041
                *((xtWord4 *) (new_ptr + size + 4)) = 0x7E7EFEFE;
 
1042
                *ptr = new_ptr+4;
 
1043
                return OK;
 
1044
        }
 
1045
        *ptr = xt_malloc(self, size);
 
1046
        return *ptr != NULL;
 
1047
}
 
1048
 
 
1049
xtPublic void xt_free(XTThreadPtr XT_UNUSED(self), void *ptr)
 
1050
{
 
1051
        char    *old_ptr;
 
1052
        xtWord4 size;
 
1053
        void    check_for_file(char *my_ptr, xtWord4 len);
 
1054
 
 
1055
        old_ptr = (char *) ptr;
 
1056
        old_ptr -= 4;
 
1057
        size = *((xtWord4 *) old_ptr);
 
1058
        if (size == 0xDEADBEAF || *((xtWord4 *) (old_ptr + size + 4)) != 0x7E7EFEFE) {
 
1059
                char *dummy = NULL;
 
1060
                
 
1061
                xt_dump_trace();
 
1062
                *dummy = 41;
 
1063
        }
 
1064
        check_for_file((char *) ptr, size);
 
1065
        *((xtWord4 *) old_ptr) = 0xDEADBEAF;
 
1066
        *((xtWord4 *) (old_ptr + size)) = 0xEFEFDFDF;
 
1067
        *((xtWord4 *) (old_ptr + size + 4)) = 0x1F1F1F1F;
 
1068
        //memset(old_ptr, 0xEF, size+4);
 
1069
        free(old_ptr);
 
1070
}
 
1071
 
 
1072
xtPublic void *xt_calloc(XTThreadPtr self, size_t size)
 
1073
{
 
1074
        void *ptr;
 
1075
 
 
1076
        if ((ptr = xt_malloc(self, size)))
 
1077
                memset(ptr, 0, size);
 
1078
        return ptr;
 
1079
}
 
1080
 
 
1081
#undef  xt_pfree
 
1082
 
 
1083
xtPublic void xt_pfree(XTThreadPtr self, void **ptr)
 
1084
{
 
1085
        if (*ptr) {
 
1086
                void *p = *ptr;
 
1087
 
 
1088
                *ptr = NULL;
 
1089
                xt_free(self, p);
 
1090
        }
 
1091
}
 
1092
 
 
1093
xtPublic void *xt_malloc_ns(size_t size)
 
1094
{
 
1095
        char *ptr;
 
1096
 
 
1097
        if (!(ptr = (char *) malloc(size+8))) {
 
1098
                xt_register_errno(XT_REG_CONTEXT, XT_ENOMEM);
 
1099
                return NULL;
 
1100
        }
 
1101
        *((xtWord4 *) ptr) = size;
 
1102
        *((xtWord4 *) (ptr + size + 4)) = 0x7E7EFEFE;
 
1103
        return ptr+4;
 
1104
}
 
1105
 
 
1106
xtPublic void *xt_calloc_ns(size_t size)
 
1107
{
 
1108
        char *ptr;
 
1109
 
 
1110
        if (!(ptr = (char *) malloc(size+8))) {
 
1111
                xt_register_errno(XT_REG_CONTEXT, XT_ENOMEM);
 
1112
                return NULL;
 
1113
        }
 
1114
        *((xtWord4 *) ptr) = size;
 
1115
        *((xtWord4 *) (ptr + size + 4)) = 0x7E7EFEFE;
 
1116
        memset(ptr+4, 0, size);
 
1117
        return ptr+4;
 
1118
}
 
1119
 
 
1120
xtPublic xtBool xt_realloc_ns(void **ptr, size_t size)
 
1121
{
 
1122
        char *old_ptr;
 
1123
        char *new_ptr;
 
1124
 
 
1125
        if ((old_ptr = (char *) *ptr)) {
 
1126
                void check_for_file(char *my_ptr, xtWord4 len);
 
1127
                
 
1128
                xt_check_ptr(old_ptr);
 
1129
                check_for_file((char *) old_ptr, *((xtWord4 *) (old_ptr - 4)));
 
1130
                if (!(new_ptr = (char *) realloc(old_ptr - 4, size+8)))
 
1131
                        return xt_register_errno(XT_REG_CONTEXT, XT_ENOMEM);
 
1132
                *((xtWord4 *) new_ptr) = size;
 
1133
                *((xtWord4 *) (new_ptr + size + 4)) = 0x7E7EFEFE;
 
1134
                *ptr = new_ptr+4;
 
1135
                return OK;
 
1136
        }
 
1137
        *ptr = xt_malloc_ns(size);
 
1138
        return *ptr != NULL;
 
1139
}
 
1140
 
 
1141
xtPublic void xt_free_ns(void *ptr)
 
1142
{
 
1143
        char    *old_ptr;
 
1144
        xtWord4 size;
 
1145
        void    check_for_file(char *my_ptr, xtWord4 len);
 
1146
 
 
1147
        old_ptr = (char *) ptr;
 
1148
        old_ptr -= 4;
 
1149
        size = *((xtWord4 *) old_ptr);
 
1150
        if (size == 0xDEADBEAF || *((xtWord4 *) (old_ptr + size + 4)) != 0x7E7EFEFE) {
 
1151
                char *dummy = NULL;
 
1152
                
 
1153
                xt_dump_trace();
 
1154
                *dummy = 42;
 
1155
        }
 
1156
        check_for_file((char *) ptr, size);
 
1157
        *((xtWord4 *) old_ptr) = 0xDEADBEAF;
 
1158
        *((xtWord4 *) (old_ptr + size)) = 0xEFEFDFDF;
 
1159
        *((xtWord4 *) (old_ptr + size + 4)) = 0x1F1F1F1F;
 
1160
        //memset(old_ptr, 0xEE, size+4);
 
1161
        free(old_ptr);
 
1162
}
 
1163
#endif
 
1164