pastebin - collaborative debugging tool
rovema.kpaste.net RSS

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

    
11
/*
22
 * custom_c++_operator_new1.cpp - custom allocator demo
33
 *
44
 * Written by Roland Mainz <roland.mainz@nrubsig.org>
55
 * Compile with:
66
 * clang++ -std=c++17 -g -Wall custom_c++_operator_new1.cpp
77
 *
88
 */
99
1010
#include <iostream>
1111
#include <memory>
1212
#include <limits>
1313
1414
1515
/* required for |main()| test/demo code */
1616
#include <unistd.h>
1717
#include <stdlib.h>
1818
#include <stdio.h>
1919
#include <string.h>
2020
#include <inttypes.h>
2121
2222
/* misc */
2323
#define STDERR_MSG(msg) \
2424
	(void)write(STDERR_FILENO, (msg), strlen(msg))
2525
2626
#define USE_STATIC_BUFFER_ALLOC 1
2727
2828
2929
#if USE_STATIC_BUFFER_ALLOC
3030
static struct
3131
{
3232
	char buffer[512];
3333
	/* more complex cases would require |pthread_once()| */
3434
	char *buf_ptr = buffer;
3535
} buf_alloc;
3636
3737
void *my_buffer_alloc(size_t size)
3838
{
3939
#define SIZE_ALIGN(size, align) \
4040
	((size)+((align)-1)-((size)-1)%(align))
4141
4242
	void *mem;
4343
	size_t bs;
4444
4545
	STDERR_MSG("my_buffer_alloc\n");
4646
4747
	bs = SIZE_ALIGN(size, 16);
4848
4949
	mem = buf_alloc.buf_ptr;
5050
	buf_alloc.buf_ptr += bs;
5151
5252
	/* could we satisfy the allocation request ? */
5353
	if ((buf_alloc.buf_ptr - buf_alloc.buffer) >= sizeof(buf_alloc.buffer))
5454
		return NULL;
5555
5656
#if 1
5757
	(void)fprintf(stderr, "my_buffer_alloc(size=%ld, bs=%ld, mem=%lx\n",
5858
		(long)size, (long)bs, (long)mem);
5959
#endif
6060
6161
	return mem;
6262
}
6363
6464
void my_buffer_dealloc(void *ptr)
6565
{
6666
	/*
6767
	 * range check - pointer must be within |buf_alloc.buffer|!
6868
	 *
6969
	 * We use this simple (and cheap) check to catch invalid
7070
	 * pointers, or (more important) pointers to memory from
7171
	 * other allocators (if we mix&match multiple different
7272
	 * allocators or different memory pools from the same
7373
	 * allocator system).
7474
	 */
7575
	ptrdiff_t x = (char *)ptr - buf_alloc.buffer;
7676
	if ((x < 0) || (x >= (ptrdiff_t)sizeof(buf_alloc.buffer))) {
7777
		/* BUG: LAZY - we should not use stdio here... */
7878
		(void)fprintf(stderr, "delete(): bad pointer %lx!\n", (long)x);
7979
	}
8080
}
8181
#endif /* USE_STATIC_BUFFER_ALLOC */
8282
8383
8484
void *
8585
operator new(std::size_t size)
8686
//     throw(std::bad_alloc)
8787
{
8888
	STDERR_MSG("##new\n");
8989
	if (size == 0)
9090
		size = 1;
9191
	void* p;
9292
#if USE_STATIC_BUFFER_ALLOC
9393
	if ((p = my_buffer_alloc(size)) == 0)
9494
#else
9595
	if ((p = ::malloc(size)) == 0)
9696
#endif /* USE_STATIC_BUFFER_ALLOC */
9797
	{
9898
		throw std::bad_alloc();
9999
	}
100100
	return p;
101101
}
102102
103103
104104
void
105105
operator delete(void* ptr) _GLIBCXX_USE_NOEXCEPT
106106
{
107107
	STDERR_MSG("##delete\n");
108108
#if USE_STATIC_BUFFER_ALLOC
109109
	my_buffer_dealloc(ptr);
110110
#else
111111
	if (ptr)
112112
		::free(ptr);
113113
#endif /* USE_STATIC_BUFFER_ALLOC */
114114
}
115115
116116
117117
/*
118118
 * static variables:
119119
 * Their memory is allocated/constructor called before calling
120120
 * |main()|, and their destructor is called and memory deallocated
121121
 * after |main()| returned.
122122
 * This causes some nasty headaches for custom |operator new|, e.g.
123123
 * in case we want to rely on stdio, C++ iostream, or other
124124
 * libc/libc++ modules. And don't even start to ask about headaches
125125
 * related to STL...
126126
 */
127127
static int *s_f = new int(59);
128128
129129
static std::string s = "xjsdsqwertzuiopasdfghjklöxcvbnm,poiuztrewlkjhgfdmnbvcxjsddljkjklsdfjklsddfljkx";
130130
131131
132132
int main(int ac, char *av[])
133133
{
134134
	(void)puts("# >> main:start.");
135135
136-
	int *f = new int(84); 41void *
136+
	int *f = new int(84);
137-
 42operator new(std::size_t size)
137+
138-
 43#if !__has_feature(cxx_noexcept)
138+
139-
 44    throw(std::bad_alloc)
139+
	std::string s = "13123123123122354245234534534534534534656456456456456456242342341233423423524542hello";
140-
 45#endif
140+
	s+=" ";
141-
 46{
141+
	s+="world";
142-
 47    if (size == 0)
142+
143-
 48        size = 1;
143+
	std::cout << s << "#" << *f << "," << *s_f << "#" << std::endl;
144-
 49    void* p;
144+
145-
 50    while ((p = ::malloc(size)) == 0)
145+
	delete f;
146-
 51    {
146+
147-
 52        // If malloc fails and there is a new_handler,
147+
#if 0
148-
 53        // call it to try free up memory.
148+
	/* test if pointer validation of |my_buffer_dealloc()| works */
149-
 54        std::new_handler nh = std::get_new_handler();
149+
	delete (int *)malloc(5);
150-
 55        if (nh)
150+
#endif
151-
 56            nh();
151+
152-
 57        else
152+
	(void)puts("# >> main:done.");
153-
 58#ifndef _LIBCPP_NO_EXCEPTIONS
153+
	return EXIT_SUCCESS;
154-
 59            throw std::bad_alloc();
154+
}
155-
 60#else
155+
156-
 61            break;
156+
157-
 62#endif
157+
158-
 63    }
158+
159-
 64    return p;
159+
160-
 65}
160+
161161
162162
163-
	std::string s = "13123123123122354245234534534534534534656456456456456456242342341233423423524542hello";
163+
164-
	s+=" ";
164+
165-
	s+="world";
165+
166166
167-
	std::cout << s << "#" << *f << "," << *s_f << "#" << std::endl;
167+
168168
169-
	delete f;
169+
170170
171-
#if 0
171+
172-
	/* test if pointer validation of |my_buffer_dealloc()| works */
172+
173-
	delete (int *)malloc(5);
173+
174-
#endif
174+
175175
176-
	(void)puts("# >> main:done.");
176+
177-
	return EXIT_SUCCESS;
177+
178-
}
178+
179179

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