pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post 776ea26 by Anonymous on Mon 15th Aug 2022 20:30
original post 4c6 by Anonymous on Mon 15th Aug 2022 20:30
Download diff
Show old version | new version | both versions

    
11
/*
2-
 * custom_c++_operator_new1.cpp - custom allocator demo
2+
 * custom_c++_operator_new2.cpp - custom allocator demo
33
 *
44
 * Written by Roland Mainz <roland.mainz@nrubsig.org>
75
 *
6+
 * - Compile with:
811
 */
912
13+
#define USE_STATIC_BUFFER_ALLOC 0
1316
17+
#define STATIC_BUFFER_ALLOC_BUFFER_SIZE (512)
1418
19+
#include <iostream>
1524
/* required for |main()| test/demo code */
1625
#include <unistd.h>
1726
#include <stdlib.h>
1827
#include <stdio.h>
1928
#include <string.h>
2029
#include <inttypes.h>
2130
31+
#if USE_TLSF_ALLOC
2237
/* misc */
2338
#define STDERR_MSG(msg) \
2439
	(void)write(STDERR_FILENO, (msg), strlen(msg))
2540
2741
2842
2943
#if USE_STATIC_BUFFER_ALLOC
44+
extern "C" {
3045
static struct
3146
{
32-
	char buffer[512];
47+
	char buffer[STATIC_BUFFER_ALLOC_BUFFER_SIZE];
3348
	/* more complex cases would require |pthread_once()| */
3449
	char *buf_ptr = buffer;
3550
} buf_alloc;
3651
3752
void *my_buffer_alloc(size_t size)
3853
{
3954
#define SIZE_ALIGN(size, align) \
4055
	((size)+((align)-1)-((size)-1)%(align))
4156
4257
	void *mem;
4358
	size_t bs;
4459
4560
	STDERR_MSG("my_buffer_alloc\n");
4661
4762
	bs = SIZE_ALIGN(size, 16);
4863
4964
	mem = buf_alloc.buf_ptr;
5065
	buf_alloc.buf_ptr += bs;
5166
5267
	/* could we satisfy the allocation request ? */
5368
	if ((buf_alloc.buf_ptr - buf_alloc.buffer) >= sizeof(buf_alloc.buffer))
5469
		return NULL;
5570
56-
#if 1
71+
#if 0
5772
	(void)fprintf(stderr, "my_buffer_alloc(size=%ld, bs=%ld, mem=%lx\n",
5873
		(long)size, (long)bs, (long)mem);
5974
#endif
6075
6176
	return mem;
6277
}
6378
6479
void my_buffer_dealloc(void *ptr)
6580
{
6681
	/*
67-
	 * range check - pointer must be within |buf_alloc.buffer|!
82+
	 * range check - pointer must point within |buf_alloc.buffer|!
6883
	 *
6984
	 * We use this simple (and cheap) check to catch invalid
7085
	 * pointers, or (more important) pointers to memory from
7186
	 * other allocators (if we mix&match multiple different
7287
	 * allocators or different memory pools from the same
7388
	 * allocator system).
7489
	 */
7590
	ptrdiff_t x = (char *)ptr - buf_alloc.buffer;
7691
	if ((x < 0) || (x >= (ptrdiff_t)sizeof(buf_alloc.buffer))) {
7792
		/* BUG: LAZY - we should not use stdio here... */
7893
		(void)fprintf(stderr, "delete(): bad pointer %lx!\n", (long)x);
7994
	}
8095
}
96+
} /* extern "C" */
8197
#endif /* USE_STATIC_BUFFER_ALLOC */
8298
8399
84100
void *
85101
operator new(std::size_t size)
86102
//     throw(std::bad_alloc)
87103
{
88104
	STDERR_MSG("##new\n");
89105
	if (size == 0)
90106
		size = 1;
91107
	void* p;
92108
#if USE_STATIC_BUFFER_ALLOC
93109
	if ((p = my_buffer_alloc(size)) == 0)
110+
#elif USE_TLSF_ALLOC
95113
	if ((p = ::malloc(size)) == 0)
114+
#else
96116
#endif /* USE_STATIC_BUFFER_ALLOC */
97117
	{
98118
		throw std::bad_alloc();
99119
	}
100120
	return p;
101121
}
102122
103123
104124
void
105125
operator delete(void* ptr) _GLIBCXX_USE_NOEXCEPT
106126
{
107127
	STDERR_MSG("##delete\n");
108128
#if USE_STATIC_BUFFER_ALLOC
109129
	my_buffer_dealloc(ptr);
130+
#elif USE_TLSF_ALLOC
111131
	if (ptr)
132+
		::tlsf_free(ptr);
112135
		::free(ptr);
136+
#else
113138
#endif /* USE_STATIC_BUFFER_ALLOC */
114139
}
115140
116141
117142
/*
118143
 * static variables:
119144
 * Their memory is allocated/constructor called before calling
120145
 * |main()|, and their destructor is called and memory deallocated
121146
 * after |main()| returned.
122147
 * This causes some nasty headaches for custom |operator new|, e.g.
123148
 * in case we want to rely on stdio, C++ iostream, or other
124149
 * libc/libc++ modules. And don't even start to ask about headaches
125-
 * related to STL...
150+
 * related to STL and C++ exceptions...
126151
 */
127152
static int *s_f = new int(59);
128153
129-
static std::string s = "xjsdsqwertzuiopasdfghjklöxcvbnm,poiuztrewlkjhgfdmnbvcxjsddljkjklsdfjklsddfljkx";
154+
static std::string s = "xjsdsqwertzuiopasdfghjklxcvbnm,poiuztrewlkjhgfdmnbvcxjsddljkjklsdfjklsddfljkx";
130155
131156
132157
int main(int ac, char *av[])
133158
{
134159
	(void)puts("# >> main:start.");
135160
136161
	int *f = new int(84);
137162
138163
139164
	std::string s = "13123123123122354245234534534534534534656456456456456456242342341233423423524542hello";
140165
	s+=" ";
141166
	s+="world";
142167
143168
	std::cout << s << "#" << *f << "," << *s_f << "#" << std::endl;
144169
145170
	delete f;
146171
147172
#if 0
148173
	/* test if pointer validation of |my_buffer_dealloc()| works */
149174
	delete (int *)malloc(5);
150175
#endif
151176
152177
	(void)puts("# >> main:done.");
153178
	return EXIT_SUCCESS;
154179
}
155180

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at