aboutsummaryrefslogtreecommitdiffstats
path: root/memory.cpp
blob: 80f8cef9718ecaf72d38d6cf9f7fd67d2b98fd6f (plain) (blame)
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
// memory.cpp
// Revision 24-apr-2009

#include "memory.h"

#include "blassic.h"
#include "error.h"
#include "trace.h"

//#ifdef HAVE_SYS_MMAN_H
#ifdef HAVE_MMAP

#include <sys/mman.h>

#if ! defined MAP_ANON

#if defined MAP_ANONYMOUS

#define MAP_ANON MAP_ANONYMOUS

#else

#error "Don't know how to flag anonymous map."

#endif

#endif

#endif
// HAVE_SYS_MMAN_H

#include <iostream>
using std::cerr;
using std::endl;

#include <string.h>
#include <errno.h>

//#include <set>
#include <map>

namespace {

//typedef std::set <void *> memused_t;
typedef std::map <void *, size_t> memused_t;

memused_t memused;

#ifdef HAVE_SYS_MMAN_H

void do_munmap (void * start, size_t length)
{
	TRACEFUNC (tr, "do_munmap");

	int r= munmap (start, length);
	if (r != 0)
	{
		const char * errormessage= strerror (errno);
		TRMESSAGE (tr, std::string ("munmap failed: ") + errormessage);
		if (showdebuginfo () )
			cerr << "munmap (" << start << ", " <<
				length << ") failed: " << errormessage <<
				endl;
	}
}

#endif

} // namespace

size_t blassic::memory::dyn_alloc (size_t memsize)
{
	TRACEFUNC (tr, "memory::alloc");

	if (memsize == 0)
		throw ErrFunctionCall;

	#ifndef HAVE_SYS_MMAN_H

	void * aux= malloc (memsize);

	#else

	void * aux= mmap (NULL, memsize, PROT_READ | PROT_WRITE | PROT_EXEC,
		MAP_PRIVATE | MAP_ANON, -1, 0);
	if (aux == (void *) -1)
		aux= NULL;

	#endif

	if (aux == NULL)
	{
		if (showdebuginfo () )
			cerr << "Failed allocation of " << memsize <<
				" bytes: " << strerror (errno) << endl;
		throw ErrOutMemory;
	}

	//memused.insert (aux);
	memused [aux]= memsize;
	return reinterpret_cast <size_t> (aux);
}

void blassic::memory::dyn_free (size_t mempos)
{
	TRACEFUNC (tr, "memory::free");

	void * aux= reinterpret_cast <void *> (mempos);
	memused_t::iterator it= memused.find (aux);
	if (it == memused.end () )
	{
		if (showdebuginfo () )
			cerr << "Trying to free address " << mempos <<
				" but is not allocated" << endl;
		throw ErrFunctionCall;
	}

	#ifdef HAVE_SYS_MMAN_H

	size_t memsize= it->second;

	#endif

	memused.erase (it);

	#ifndef HAVE_SYS_MMAN_H

	::free (aux);

	#else

	//munmap (aux, memsize);
	do_munmap (aux, memsize);

	#endif
}

void blassic::memory::dyn_freeall ()
{
	TRACEFUNC (tr, "memory::freeall");

	bool something= false;
	while (! memused.empty () )
	{
		something= true;
		memused_t::iterator it= memused.begin ();

		#ifndef HAVE_SYS_MMAN_H

		::free (it->first);

		#else

		//munmap (it->first, it->second);
		do_munmap (it->first, it->second);

		#endif

		memused.erase (it);
	}

	if (! something)
		TRMESSAGE (tr, "Nothing to free");
}

// End of memory.cpp
Un proyecto texto-plano.xyz