Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative offset pointers that store array length? #266

Closed
zezba9000 opened this issue Mar 1, 2019 · 3 comments
Closed

Negative offset pointers that store array length? #266

zezba9000 opened this issue Mar 1, 2019 · 3 comments

Comments

@zezba9000
Copy link

zezba9000 commented Mar 1, 2019

Does the bdwgc support negative offset pointers? Trying to avoid ptr arithmetic by storing the length of an array at a negative offset.

void* IL2X_GC_New(size_t size)
{
	void* ptr = GC_malloc(size);
	memset(ptr, 0, size);
	return ptr;
}

void* IL2X_GC_NewArray(size_t size)
{
	char* result = IL2X_GC_New(sizeof(size_t) + size);
	*((size_t*)result) = size;
	result += sizeof(size_t);// could the GC handle this
	return result;
}

int ArrayLength(void* arrayPtr)
{
  return (int)*(((char*)arrayPtr) - size_t);
}

void Foo()
{
  int* array = IL2X_GC_NewArray(128);
  int length = ArrayLength(array);
  array[22] = 0;// trying to optimize with this approch
  (int*)(((char*)array) + size_t)[22] = 0;// really don't want to have to do this
}
@ivmai
Copy link
Owner

ivmai commented Mar 1, 2019

memset(ptr, 0, size);

Not needed. GC_malloc returns zeroed memory.

result += sizeof(size_t);// could the GC handle this?

Yes.

return (int)(((char)arrayPtr) - size_t);

A cast to size_t* is missing (you store 4 bytes but read only 1, in case of big-endian architecture it is definitely a bug).

array[22] = 0;// trying to optimize with this approach

It is OK (it should work).

PS. It is better to ask question on Stackoveflow: https://stackoverflow.com/questions/tagged/boehm-gc

@ivmai ivmai closed this as completed Mar 1, 2019
@zezba9000
Copy link
Author

zezba9000 commented Mar 1, 2019

return (int)*(((char*)arrayPtr) - sizeof(size_t));

Was just a type-o, just pseudocode.

result += sizeof(size_t);
Yes

So the GC wont collect the allocated memory even when nothing is directly pointing to it on the heap or stack? The GC supports checking if any ptr is pointing inside a heap object not just its ref directly? Just clarifying.

Will ask future question on stack overflow.

@ivmai
Copy link
Owner

ivmai commented Mar 1, 2019

So the GC wont collect the allocated memory even when nothing is directly pointing to it on the heap or stack? The GC supports checking if any ptr is pointing inside a heap object not just its ref directly?

Yes, but you need to instruct GC at the program start to do so either by calling GC_register_displacement(sizeof(void*)) (allow pointer to have a word-size offset) or GC_set_all_interior_pointers(1) (allow pointers to any offset inside object).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants