Skip to content

Instantly share code, notes, and snippets.

@MasterDuke17
Last active September 12, 2017 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasterDuke17/9bd82ae1823f271f7e83d00210817b32 to your computer and use it in GitHub Desktop.
Save MasterDuke17/9bd82ae1823f271f7e83d00210817b32 to your computer and use it in GitHub Desktop.
VMArray FSA support thoughts/questions
diff --git a/src/core/fixedsizealloc.c b/src/core/fixedsizealloc.c
index 1c84a7247..d1729e0b2 100644
--- a/src/core/fixedsizealloc.c
+++ b/src/core/fixedsizealloc.c
@@ -203,6 +203,44 @@ void * MVM_fixed_size_alloc_zeroed(MVMThreadContext *tc, MVMFixedSizeAlloc *al,
return allocd;
}
+/* Reallocs a piece of memory to the specified size, using the FSA. */
+void * MVM_fixed_size_realloc(MVMThreadContext *tc, MVMFixedSizeAlloc *al, void * p, size_t old_bytes, size_t new_bytes) {
+ MVMuint32 old_bin = bin_for(old_bytes);
+ MVMuint32 new_bin = bin_for(new_bytes);
+ if (old_bin == new_bin) {
+ return p;
+ }
+ else if (old_bin < MVM_FSA_BINS || new_bin < MVM_FSA_BINS) {
+ void *allocd = MVM_fixed_size_alloc(tc, al, new_bytes);
+ memcpy(allocd, p, new_bin > old_bin ? old_bytes : new_bytes);
+ MVM_fixed_size_free(tc, al, old_bytes, p);
+ return allocd;
+ }
+ else {
+ MVM_realloc(p, new_bytes);
+ return p;
+ }
+}
+
+/* Reallocs a piece of memory to the specified size, using the FSA. */
+void * MVM_fixed_size_realloc_at_safepoint(MVMThreadContext *tc, MVMFixedSizeAlloc *al, void * p, size_t old_bytes, size_t new_bytes) {
+ MVMuint32 old_bin = bin_for(old_bytes);
+ MVMuint32 new_bin = bin_for(new_bytes);
+ if (old_bin == new_bin) {
+ return p;
+ }
+ else if (old_bin < MVM_FSA_BINS || new_bin < MVM_FSA_BINS) {
+ void *allocd = MVM_fixed_size_alloc(tc, al, new_bytes);
+ memcpy(allocd, p, new_bin > old_bin ? old_bytes : new_bytes);
+ MVM_fixed_size_free_at_safepoint(tc, al, old_bytes, p);
+ return allocd;
+ }
+ else {
+ MVM_realloc(p, new_bytes);
+ return p;
+ }
+}
+
/* Frees a piece of memory of the specified size, using the FSA. */
static void add_to_global_bin_freelist(MVMThreadContext *tc, MVMFixedSizeAlloc *al,
MVMint32 bin, void *to_free) {
diff --git a/src/core/fixedsizealloc.h b/src/core/fixedsizealloc.h
index 94ec79bf6..e8bf8a22d 100644
--- a/src/core/fixedsizealloc.h
+++ b/src/core/fixedsizealloc.h
@@ -99,6 +99,8 @@ MVMFixedSizeAlloc * MVM_fixed_size_create(MVMThreadContext *tc);
void MVM_fixed_size_create_thread(MVMThreadContext *tc);
void * MVM_fixed_size_alloc(MVMThreadContext *tc, MVMFixedSizeAlloc *fsa, size_t bytes);
void * MVM_fixed_size_alloc_zeroed(MVMThreadContext *tc, MVMFixedSizeAlloc *fsa, size_t bytes);
+void * MVM_fixed_size_realloc(MVMThreadContext *tc, MVMFixedSizeAlloc *al, void * p, size_t old_bytes, size_t new_bytes);
+void * MVM_fixed_size_realloc_at_safepoint(MVMThreadContext *tc, MVMFixedSizeAlloc *al, void * p, size_t old_bytes, size_t new_bytes);
void MVM_fixed_size_destroy(MVMFixedSizeAlloc *al);
void MVM_fixed_size_destroy_thread(MVMThreadContext *tc);
void MVM_fixed_size_free(MVMThreadContext *tc, MVMFixedSizeAlloc *fsa, size_t bytes, void *free);
For VMArray to be implemented with the FSA, the FSA needs to support realloc. MVM_realloc is used in VMArray's set_size_internal
to grow (but never shrink) an array, but since other use cases may want to shrink the memory, I'll discuss those cases also. I
believe there are several cases to consider:
1. the original size was small enough to have used the FSA
a. the new size fits within the same bin - do nothing
b. the new size is bigger, but fits within the max bin size - MVM_fixed_size_alloc with the new size, memcpy the size of the
old bin to the new bin, MVM_fixed_size_free(_at_safepoint)? the old bin
c. the new size is bigger than the max bin size - do 1.b because MVM_fixed_size_alloc already handles this case
d. the new size fits within a smaller bin - do nothing? or MVM_fixed_size_alloc with the new size, memcpy the size of the new
bin to the new bin, MVM_fixed_size_free(_at_safepoint)? the old bin
2. the original size is bigger than the max bin size
a. the new size is bigger - just do an MVM_realloc
b. the new size fits within the max bin size - just do an MVM_realloc? or MVM_fixed_size_alloc with the new size, memcpy the
size of the new bin to the new bin, MVM_free the old memory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment