Debian Codegraph @

vendor/Heap-Layers/examples/kingsley/libkingsley.cpp

raw
1 /* -*- C++ -*- */
2
3 /*
4
5 Heap Layers: An Extensible Memory Allocation Infrastructure
6
7 Copyright (C) 2000-2017 by Emery Berger
8 http://www.emeryberger.com
9 [email protected]
10
11 */
12
13 /*
14 * @file libkingsley.cpp
15 * @brief This file replaces malloc etc. in your application.
16 * @author Emery Berger <http://www.emeryberger.com>
17 */
18
19 #include <stdlib.h>
20
21 volatile int anyThreadCreated = 1;
22
23 #include "heaplayers.h"
24
25
26 using namespace HL;
27
28 class TopHeap : public SizeHeap<UniqueHeap<ZoneHeap<MmapHeap, 65536> > > {};
29
30 class TheCustomHeapType :
31 public ANSIWrapper<KingsleyHeap<AdaptHeap<DLList, TopHeap>, TopHeap> > {};
32
33 inline static TheCustomHeapType * getCustomHeap() {
34 static char thBuf[sizeof(TheCustomHeapType)];
35 static TheCustomHeapType * th = new (thBuf) TheCustomHeapType;
36 return th;
37 }
38
39 #if defined(_WIN32)
40 #pragma warning(disable:4273)
41 #endif
42
43 #include "printf.h"
44
45 #if !defined(_WIN32)
46 #include <unistd.h>
47
48 extern "C" {
49 // For use by the replacement printf routines (see
50 // https://github.com/emeryberger/printf)
51 void _putchar(char ch) { ::write(1, (void *)&ch, 1); }
52 }
53 #endif
54
55 #include "wrappers/generic-memalign.cpp"
56
57 extern "C" {
58
59 void * xxmalloc (size_t sz) {
60 // printf_("xxmalloc\n");
61 auto ptr = getCustomHeap()->malloc (sz);
62 // printf_("xxmalloc %lu = %p\n", sz, ptr);
63 return ptr;
64 }
65
66 void xxfree (void * ptr) {
67 getCustomHeap()->free (ptr);
68 }
69
70 void * xxmemalign(size_t alignment, size_t sz) {
71 return generic_xxmemalign(alignment, sz);
72 }
73
74 size_t xxmalloc_usable_size (void * ptr) {
75 return getCustomHeap()->getSize (ptr);
76 }
77
78 void xxmalloc_lock() {
79 // getCustomHeap()->lock();
80 }
81
82 void xxmalloc_unlock() {
83 // getCustomHeap()->unlock();
84 }
85
86 }
87