Skip to content

Buffers and allocators #15

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

Open
wants to merge 3 commits into
base: core
Choose a base branch
from

Conversation

andrewCodeDev
Copy link
Collaborator

Added null_allocator, regular_buffer, irregular_buffer, and updated existing allocators.

Comment on lines +1 to +9
// The NullAllocator is meant to be used as a fallback
// for allocator composition.

// For example, a StackAllocator, if it runs out of memory,
// can dispatch to another allocator to fulfill the request.
// If we want that to signal an error, we can give it the
// NullAllocator as a fallback and that will signal an
// "OutOfMemory" error, enforcing that we don't ask for
// more memory than is on the stack.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually pretty genius!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also possible to use this for something like an allocator that can be "turned off" by mutating itself and replacing its composed allocator with this allocator.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, now that's an interesting idea. Since we can access the member variables directly in Zig, I think this is something that we can already express?

Comment on lines +252 to +263
backing_allocator: std.mem.Allocator,

// TODO: Create a dummy mutex that can be swapped via policy
mutex: std.Thread.Mutex = std.Thread.Mutex{ },

pub fn init(fallback: std.mem.Allocator) Self {
return Self {
.backing_allocator = fallback,
.buffer = OrderedCache.init(std.heap.page_allocator)
};
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome

Comment on lines 49 to 51
pub fn itemCount(self: *const Self) usize {
return self.idxs.len;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about this as a convention?

  • len -- Collection is linear (array/list/etc) and size is known O(1)
  • size -- Collection size is statically known O(1)
  • count() -- Collection size will be dynamically computed at O(n)

Whether len or size are struct members or functions depends on the details

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea! I honestly ran out of synonyms at that point.

So the reason I did itemCount in this case is because I did the same thing in the RegularBuffer (but that one has a good reason to exist... it's calculated on the fly so a division operation has to occur).

Okay, so in that case... the RegularBuffer should have a count() function, and this one should have a len() function?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, and probably we should start a CONVENTIONS.md or something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here's my proposal:

len - refers to retrieving a data member that continually keeps track of the number of elements

size - refers to retrieving a non-mutable (or statically known) number of elements

count - refers to anything that must calculate the number of elements regardless of complexity (it's literally counting)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops I never responded... I think that's great

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

Successfully merging this pull request may close these issues.

2 participants