From 027f9648e00ba40c1e64da0827c93299106503dc Mon Sep 17 00:00:00 2001 From: dam Date: Fri, 10 Feb 2023 19:35:41 +0000 Subject: Temporary code to check for memory owners. --- ttt.jai | 82 +++++++++++++++++--------------------------------------------- unused.jai | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 61 deletions(-) diff --git a/ttt.jai b/ttt.jai index fc2e5a3..345e3cc 100644 --- a/ttt.jai +++ b/ttt.jai @@ -1226,68 +1226,30 @@ main :: () { } app_directory = join(app_directory, "/", APP_FOLDER_NAME, allocator = temp); // TODO Change to default allocator because we want to keep this during runtime. - // TODO DEBUG - // Check what's going on with the temp allocator: - // Is it really the responsible for these paths? - // It seems that the next beta (after 0.1.055b) compiler allows us to check this pretty easily. - /* - // - // An example that uses several different allocators, then asks them all - // who owns which memory. - // - // Note that this is probably not the kind of thing you want to do at runtime - // in the steady state, as it may not be very fast, but it could be a very helpful - // debugging facility. - // - - - #import "Basic"; - #import "Pool"; - #import "Flat_Pool"; - #import "rpmalloc"; - - main :: () { - pool: Pool; - flat: Flat_Pool; - - a := context.default_allocator; - b := Allocator.{pool_allocator_proc, *pool}; - c := Allocator.{flat_pool_allocator_proc, *flat}; - d := Allocator.{rpmalloc_allocator_proc, null}; - - d.proc(.STARTUP, 0, 0, null, null); // rpmalloc needs explicit init right now, but others don't. - - ma := alloc(1000, allocator=a); - mb := alloc(1000, allocator=b); - mc := alloc(1000, allocator=c); - md := alloc(1000, allocator=d); - - report_who_owns(ma, a, b, c, d); - report_who_owns(mb, a, b, c, d); - report_who_owns(mc, a, b, c, d); - report_who_owns(md, a, b, c, d); - } - - report_who_owns :: (memory: *void, allocators: .. Allocator) { - someone_owns_this := false; - - print("Querying all allocators for address: %\n", memory); - - for allocators { - caps, name := get_capabilities(it); - assert((caps & .IS_THIS_YOURS) != 0); // It had better be claiming to support this! - - yours := cast(bool) it.proc(.IS_THIS_YOURS, 0, 0, memory, it.data); - print("[%] says \"%\"\n", yours, name); - - someone_owns_this ||= yours; - } - - assert(someone_owns_this); - } +// print(">%:%:%\n", temp, temp.data, context.temporary_storage.data); + print("#%\n", <%:%\n", app_directory.count, *app_directory.data); +// free(app_directory); + + /* WIP TEST THIS +some_pointer : *void = get_something(); +is_it_mine : bool = xx allocator.allocator_proc(.IS_THIS_YOURS, 0, 0, some_pointer, null); */ -// free(app_directory); + + reset_temporary_storage(); + a := join("wow", "testsesesrserse", "2222222222222222"); + b := join("wow", "testsesesrserse", "2222222222222222"); + c := join("wow", "testsesesrserse", "2222222222222222"); + d := join("wow", "testsesesrserse", "2222222222222222"); print("bazinga: '%'\n", app_directory); // if make_directory_if_it_does_not_exist(app_directory, false) == false { // print_error("Failed to create app directory '%'.", app_directory); diff --git a/unused.jai b/unused.jai index 3529a46..04997c5 100644 --- a/unused.jai +++ b/unused.jai @@ -1,3 +1,71 @@ + + + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- // + + +// Check what's going on with the temp allocator: +// Is it really the responsible for these paths? +// It seems that the next beta (after 0.1.055b) compiler allows us to check this pretty easily. +// +// +// An example that uses several different allocators, then asks them all +// who owns which memory. +// +// Note that this is probably not the kind of thing you want to do at runtime +// in the steady state, as it may not be very fast, but it could be a very helpful +// debugging facility. +// + +#import "Basic"; +#import "Pool"; +#import "Flat_Pool"; +#import "rpmalloc"; + +main :: () { + pool: Pool; + flat: Flat_Pool; + + a := context.default_allocator; + b := Allocator.{pool_allocator_proc, *pool}; + c := Allocator.{flat_pool_allocator_proc, *flat}; + d := Allocator.{rpmalloc_allocator_proc, null}; + + d.proc(.STARTUP, 0, 0, null, null); // rpmalloc needs explicit init right now, but others don't. + + ma := alloc(1000, allocator=a); + mb := alloc(1000, allocator=b); + mc := alloc(1000, allocator=c); + md := alloc(1000, allocator=d); + + report_who_owns(ma, a, b, c, d); + report_who_owns(mb, a, b, c, d); + report_who_owns(mc, a, b, c, d); + report_who_owns(md, a, b, c, d); +} + +report_who_owns :: (memory: *void, allocators: .. Allocator) { + someone_owns_this := false; + + print("Querying all allocators for address: %\n", memory); + + for allocators { + caps, name := get_capabilities(it); + assert((caps & .IS_THIS_YOURS) != 0); // It had better be claiming to support this! + + yours := cast(bool) it.proc(.IS_THIS_YOURS, 0, 0, memory, it.data); + print("[%] says \"%\"\n", yours, name); + + someone_owns_this ||= yours; + } + + assert(someone_owns_this); +} + + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- // + + checked_add :: (a: $T, b: T) -> result: T, overflow: bool #modify { if T.type == .INTEGER return; @@ -47,4 +115,3 @@ checked_sub :: (a: $T, b: T) -> result: T, overflow: bool return result, overflow; } - -- cgit v1.2.3