Question on internal workings of Allocate

General FreeBASIC programming questions.
Post Reply
wallyg
Posts: 279
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Question on internal workings of Allocate

Post by wallyg »

When I call Allocate like

Code: Select all

          dim as any ptr	p = Allocate(some value)
The system must store the requested amount and/or the amount allocated to this area. This would be required for

Code: Select all

          Delete p
To work properly.

My question is whether I can get access to this value(s), for reading purposes only. If so how can I safely access this value(s)?

Thank you.
Imortis
Moderator
Posts: 1980
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Question on internal workings of Allocate

Post by Imortis »

You shouldn't be mixing Allocate/DeAllocate and New/Delete. If you use Allocate, also use deallocate. If you use New. also use Delete.

Allocate allocates memory of the size you request and spits out a pointer. DaAllocate takes a pointer and marks it invalid. It doesn't destroy anything, which is why the manual says to clear a string before dallocating a pointer to a string.

New and Delete require a pointer to a type, so the type can can be determined with "TypeOf" but that is not really required for new/delete.

What exactly are you trying to do? I fear you are getting into a mess.
wallyg
Posts: 279
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Question on internal workings of Allocate

Post by wallyg »

Sorry, I meant Deallocate not Delete.

I am just wondering since when you call Deallocate, you only indicate the start of the block of memory to return to the system. I figured the system would need to know the amount of memory to return so it must be stored somewhere.

Therefore subsequent to the Allocate call, given a memory address that was allocated, can I find out how much was originally allocated?
jevans4949
Posts: 1188
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: Question on internal workings of Allocate

Post by jevans4949 »

I seem to remember that I discovered the length is in the qword before the pointer which you get back. Don't think this is publicised anywhere You could test fairly easily whether this is the case.
coderJeff
Site Admin
Posts: 4386
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Question on internal workings of Allocate

Post by coderJeff »

wallyg wrote: Sep 30, 2023 6:31 Therefore subsequent to the Allocate call, given a memory address that was allocated, can I find out how much was originally allocated?
fbc's allocate/callocate/deallocate/reallocate eventually call malloc/calloc/free/realloc in the C runtime (CRT).

For windows and linux, something like:

Code: Select all

extern "c"
	#if defined(__FB_WIN32__)
		#ifdef __FB_64BIT__
			declare function getBufferSize cdecl alias "_msize" _
				( byval p as any ptr ) as ulongint
		#else
			declare function getBufferSize cdecl alias "_msize" _
				( byval p as any ptr ) as ulong
		#endif
	#elseif defined(__FB_LINUX__)
		#ifdef __FB_64BIT__
			declare function getBufferSize cdecl alias "malloc_usable_size" _
				( byval p as any ptr ) as ulongint
		#else
			declare function getBufferSize cdecl alias "malloc_usable_size" _
				( byval p as any ptr ) as ulong
		#endif
	#else
		#error getBufferSize not implemented
	#endif
end extern

'' test

var a = allocate(100)
var b = allocate(200)
var c = allocate(999)

print getBufferSize(a) 
print getBufferSize(b)
print getBufferSize(c)

sleep
But, there's no guarantee that the CRT implements this and it is different on every platform.

If you search the forum for "_msize" or "malloc_usable_size" and you can see a couple of other places this was mentioned.
Post Reply