#Having trouble with creating a isolated implementation for a General Allocator for my GC

28 messages · Page 1 of 1 (latest)

rapid musk
#

I am currently trying to implement a general allocator for my garbage collector project so you can create different allocators for different purposes, and have multiple allocators in one allocator (eg. a slab allocator). Eventually still have the garbage collector be able to manage it.

I've had a bunch of trouble with function pointers and trying to isolate the internal routines of warena.c, only exposing them through a allocator struct.

And currently im facing some issues with my arena_t not being avalible for linking outside of warena.c which I need for what im doing right now. Writing tests. But I am just not sure how to expose it.

But the big problem right now is i'm a little lost in errors and having tried a few things that didn't work like:

  • Declaring the struct as extern (Current situation errors below)
  • Trying to use typedef in different ways but getting an error trying to use an incomplete typedef

make tests - to run exactly what im running

https://github.com/seaaldev/WallyGC/tree/main

GitHub

A Work-In-Progress garbage collection libary, made for self-education! - seaaldev/WallyGC

robust frigateBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

rapid musk
#

Related Files

  • warena.c & warena.h
  • test-warena-api.c
rose cedar
#

Ah, but actually you maybe want to do:

typedef struct arena_t arena_t;
struct arena_t;
```in the `.h` file
#

But

extern struct arena_t {
  size_t      size;
  size_t allocated;
};
```really doesn't make much sense. It's not `extern`. You define it right here.
rapid musk
#

I had tried something similar as what you suggested and it failed. Now I'm getting the same errors with your suggestion hmmm

rose cedar
# rapid musk Would you mind explaining why it doesn't make sense? As far as my understanding...

extern means external linkage which is the opposite of internal linkage.
Internal linkage essentially means that each translation unit (i.e. each .c file that you compile), has it's own copy of that part, but external linkage means that all files share a single variable.

If you want to read up on it yourself, try this here: https://en.cppreference.com/w/c/language/storage_class_specifiers.html and scroll down to "Linkage and libraries". I do have to admit that it's probably very confusing to read though, but it does provide an example which may help

rose cedar
rapid musk
rapid musk
rose cedar
# rapid musk it is now

I'm not on Linux rn, but what if you do extern struct arena_t; in your warena.h instead of struct arena_t;?

rapid musk
rose cedar
# rapid musk

ah, that error should disappear if you switch the order of the typedef struct arena_t arena_t; and the extern struct arena_t;

#

Let's see what error it spits out now

rapid musk
rose cedar
#

lemme experiment a bit

rapid musk
rose cedar
# rapid musk Let it rip i will keep reading that reference article 😄

Okay, so I completely overlooked that this is not a variable but a type definition doesn't need an extern because it's really compile-time only and afterwards doesn't really matter anymore, whereas for variables it's important where they live.
You would only need extern if you wanted to declare a global struct variable.

As for your test code, you can either just move your struct arena_t definition to inside the .h file in order to expose the struct with its fields, or you can add functions to the warena.h file that would act as a proxy/middleman for these operations, if you want to keep the struct definition completely hidden

#

So if you write

typedef struct arena_t arena_t;
struct arena_t {
    size_t      size;
    size_t allocated;
};
```in your `.h` file and remove the 
```c
struct arena_t {
    size_t      size;
    size_t allocated;
};
```part from your `.c` file, then you will get different errors
rapid musk
rose cedar
#

good luck

robust frigateBOT
#

@rapid musk Has your question been resolved? If so, type !solved :)

rapid musk
#

!solved