digitalmars.D.learn - Malloc in D
- Marcin (93/93) Apr 27 2012 Hi!
- Dejan Lekic (11/104) Apr 27 2012 You probably have a very old GDC...
- Marcin (2/110) Apr 27 2012
Hi!
I find code:
[code]
import c.stdlib;
import c.stdlib;
import std.outofmemory;
class Foo{
static void[] buffer;
static int bufindex;
static const int bufsize = 100;
static this(){
void *p;
p = malloc(bufsize);
if (!p)
throw new OutOfMemory;
gc.addRange(p, p + bufsize);
buffer = p[0 .. bufsize];
}
static ~this(){
if (buffer.length){
gc.removeRange(buffer);
free(buffer);
buffer = null;
}
}
new(uint sz){
void *p;
p = &buffer[bufindex];
bufindex += sz;
if (bufindex > buffer.length)
throw new OutOfMemory;
return p;
}
delete(void* p){
assert(0);
}
static int mark(){
return bufindex;
}
static void release(int i){
bufindex = i;
}
}
void test(){
int m = Foo.mark();
Foo f1 = new Foo; // allocate
Foo f2 = new Foo; // allocate
Foo.release(m); // deallocate f1 and f2
}
[/code]
When I try compile i have some error:[code]
vander vander-VirtualBox:~/Pulpit/D$ gdc Memory.d -o Memory
Memory.d:1: Error: module stdlib is in file 'c/stdlib.d' which
cannot be read
import path[0] =
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.4.5/../../../../../include/d/4.4.5/i686-linux-gnu
import path[1] =
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.4.5/../../../../../include/d/4.4.5
vander vander-VirtualBox:~/Pulpit/D$[/code]
When i comment import c.stdlib;
I have error
[code]
vander vander-VirtualBox:~/Pulpit/D$ gdc Memory.d -o Memory
Memory.d:11: Error: undefined identifier malloc
Memory.d:11: Error: function expected before (), not __error of
type _error_
Memory.d:13: Error: identifier 'OutOfMemory' is not defined
Memory.d:13: Error: OutOfMemory is used as a type
Memory.d:13: Error: new can only create structs, dynamic arrays
or class objects, not void's
Memory.d:13: Error: can only throw class objects, not type void*
Memory.d:14: Error: undefined identifier gc, did you mean static
import gcc?
Error: no property 'addRange' for type 'TOK149'
Memory.d:14: Error: function expected before (), not __error of
type TOK149
Memory.d:19: Error: undefined identifier gc, did you mean static
import gcc?
Error: no property 'removeRange' for type 'TOK149'
Memory.d:19: Error: function expected before (), not __error of
type TOK149
Memory.d:20: Error: undefined identifier free, did you mean
function fread?
Memory.d:20: Error: function expected before (), not __error of
type _error_
Memory.d:29: Error: identifier 'OutOfMemory' is not defined
Memory.d:29: Error: OutOfMemory is used as a type
Memory.d:29: Error: new can only create structs, dynamic arrays
or class objects, not void's
Memory.d:29: Error: can only throw class objects, not type void*
vander vander-VirtualBox:~/Pulpit/D$
[/code]
What's can be wrong?
Apr 27 2012
On Friday, 27 April 2012 at 09:05:12 UTC, Marcin wrote:
Hi!
I find code:
[code]
import c.stdlib;
import c.stdlib;
import std.outofmemory;
class Foo{
static void[] buffer;
static int bufindex;
static const int bufsize = 100;
static this(){
void *p;
p = malloc(bufsize);
if (!p)
throw new OutOfMemory;
gc.addRange(p, p + bufsize);
buffer = p[0 .. bufsize];
}
static ~this(){
if (buffer.length){
gc.removeRange(buffer);
free(buffer);
buffer = null;
}
}
new(uint sz){
void *p;
p = &buffer[bufindex];
bufindex += sz;
if (bufindex > buffer.length)
throw new OutOfMemory;
return p;
}
delete(void* p){
assert(0);
}
static int mark(){
return bufindex;
}
static void release(int i){
bufindex = i;
}
}
void test(){
int m = Foo.mark();
Foo f1 = new Foo; // allocate
Foo f2 = new Foo; // allocate
Foo.release(m); // deallocate f1 and f2
}
[/code]
When I try compile i have some error:[code]
vander vander-VirtualBox:~/Pulpit/D$ gdc Memory.d -o Memory
Memory.d:1: Error: module stdlib is in file 'c/stdlib.d' which
cannot be read
import path[0] =
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.4.5/../../../../../include/d/4.4.5/i686-linux-gnu
import path[1] =
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.4.5/../../../../../include/d/4.4.5
vander vander-VirtualBox:~/Pulpit/D$[/code]
When i comment import c.stdlib;
I have error
[code]
vander vander-VirtualBox:~/Pulpit/D$ gdc Memory.d -o Memory
Memory.d:11: Error: undefined identifier malloc
Memory.d:11: Error: function expected before (), not __error of
type _error_
Memory.d:13: Error: identifier 'OutOfMemory' is not defined
Memory.d:13: Error: OutOfMemory is used as a type
Memory.d:13: Error: new can only create structs, dynamic arrays
or class objects, not void's
Memory.d:13: Error: can only throw class objects, not type void*
Memory.d:14: Error: undefined identifier gc, did you mean
static import gcc?
Error: no property 'addRange' for type 'TOK149'
Memory.d:14: Error: function expected before (), not __error of
type TOK149
Memory.d:19: Error: undefined identifier gc, did you mean
static import gcc?
Error: no property 'removeRange' for type 'TOK149'
Memory.d:19: Error: function expected before (), not __error of
type TOK149
Memory.d:20: Error: undefined identifier free, did you mean
function fread?
Memory.d:20: Error: function expected before (), not __error of
type _error_
Memory.d:29: Error: identifier 'OutOfMemory' is not defined
Memory.d:29: Error: OutOfMemory is used as a type
Memory.d:29: Error: new can only create structs, dynamic arrays
or class objects, not void's
Memory.d:29: Error: can only throw class objects, not type void*
vander vander-VirtualBox:~/Pulpit/D$
[/code]
What's can be wrong?
You probably have a very old GDC...
1) GC is a Struct inside core.memory so you need a line:
GC gc;
2) You need to throw OutOfMemoryError which is defined in
core.exception.
3) You need const(void*) pointers for GC methods.
So I first recommend you install latest GDC, and then modify your
code to be more up-to-date.
I hope this helps.
Regards :)
Apr 27 2012
Thanks for you help:) On Friday, 27 April 2012 at 09:20:04 UTC, Dejan Lekic wrote:On Friday, 27 April 2012 at 09:05:12 UTC, Marcin wrote:Hi! I find code: [code] import c.stdlib; import c.stdlib; import std.outofmemory; class Foo{ static void[] buffer; static int bufindex; static const int bufsize = 100; static this(){ void *p; p = malloc(bufsize); if (!p) throw new OutOfMemory; gc.addRange(p, p + bufsize); buffer = p[0 .. bufsize]; } static ~this(){ if (buffer.length){ gc.removeRange(buffer); free(buffer); buffer = null; } } new(uint sz){ void *p; p = &buffer[bufindex]; bufindex += sz; if (bufindex > buffer.length) throw new OutOfMemory; return p; } delete(void* p){ assert(0); } static int mark(){ return bufindex; } static void release(int i){ bufindex = i; } } void test(){ int m = Foo.mark(); Foo f1 = new Foo; // allocate Foo f2 = new Foo; // allocate Foo.release(m); // deallocate f1 and f2 } [/code] When I try compile i have some error:[code] vander vander-VirtualBox:~/Pulpit/D$ gdc Memory.d -o Memory Memory.d:1: Error: module stdlib is in file 'c/stdlib.d' which cannot be read import path[0] = /usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.4.5/../../../../../include/d/4.4.5/i686-linux-gnu import path[1] = /usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.4.5/../../../../../include/d/4.4.5 vander vander-VirtualBox:~/Pulpit/D$[/code] When i comment import c.stdlib; I have error [code] vander vander-VirtualBox:~/Pulpit/D$ gdc Memory.d -o Memory Memory.d:11: Error: undefined identifier malloc Memory.d:11: Error: function expected before (), not __error of type _error_ Memory.d:13: Error: identifier 'OutOfMemory' is not defined Memory.d:13: Error: OutOfMemory is used as a type Memory.d:13: Error: new can only create structs, dynamic arrays or class objects, not void's Memory.d:13: Error: can only throw class objects, not type void* Memory.d:14: Error: undefined identifier gc, did you mean static import gcc? Error: no property 'addRange' for type 'TOK149' Memory.d:14: Error: function expected before (), not __error of type TOK149 Memory.d:19: Error: undefined identifier gc, did you mean static import gcc? Error: no property 'removeRange' for type 'TOK149' Memory.d:19: Error: function expected before (), not __error of type TOK149 Memory.d:20: Error: undefined identifier free, did you mean function fread? Memory.d:20: Error: function expected before (), not __error of type _error_ Memory.d:29: Error: identifier 'OutOfMemory' is not defined Memory.d:29: Error: OutOfMemory is used as a type Memory.d:29: Error: new can only create structs, dynamic arrays or class objects, not void's Memory.d:29: Error: can only throw class objects, not type void* vander vander-VirtualBox:~/Pulpit/D$ [/code] What's can be wrong?You probably have a very old GDC... 1) GC is a Struct inside core.memory so you need a line: GC gc; 2) You need to throw OutOfMemoryError which is defined in core.exception. 3) You need const(void*) pointers for GC methods. So I first recommend you install latest GDC, and then modify your code to be more up-to-date. I hope this helps. Regards :)
Apr 27 2012








"Marcin" <m_zap o2.pl>