From b84412b1a51c116b237d5ebe1b00e636bbd2cf05 Mon Sep 17 00:00:00 2001 From: Tom Wallis Date: Tue, 7 Nov 2023 17:59:24 +0000 Subject: [PATCH] Increment `numCompartments` on its own line Andrei noted that incrementing `numCompartments` is more readable if done on its own line, rather than when indexing an array, in [PR review](https://github.com/capablevms/cheri-examples/pull/88#discussion_r1383710527). Thanks for the feedback! --- .../compartment_alloc/compartment_alloc.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/example_allocators/compartment_alloc/compartment_alloc.c b/example_allocators/compartment_alloc/compartment_alloc.c index 8c2ca05..1cf750a 100644 --- a/example_allocators/compartment_alloc/compartment_alloc.c +++ b/example_allocators/compartment_alloc/compartment_alloc.c @@ -17,6 +17,9 @@ int numCompartments = 0; void *__capability init_compartment(size_t size_in_bytes, void *__capability dc) { + void *buf; + void *__capability compartment_id; + // We impose a limit on our # of compartments if (numCompartments + 1 == maxCompartments) { perror("Too many compartments requested."); @@ -24,7 +27,7 @@ void } // Allocate memory for this compartment - void *buf = mmap(NULL, size_in_bytes, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); + buf = mmap(NULL, size_in_bytes, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); if (buf == MAP_FAILED) { perror("error in initial mem allocation"); @@ -32,16 +35,18 @@ void } // Create the new compartment + compartment_id = sealed_reference(buf); compartments[numCompartments] = (heap_compartment){ - buf, // Space on the heap - 0, // bytes allocated (currently none!) - size_in_bytes, // Our maximum size, to check when allocating - dc, // The data capability to allocate against (like a DDC) - sealed_reference(buf) // An identifier which allows a holder to allocate in this component + buf, // Space on the heap + 0, // bytes allocated (currently none!) + size_in_bytes, // Our maximum size, to check when allocating + dc, // The data capability to allocate against (like a DDC) + compartment_id // An identifier which allows a holder to allocate in this component }; + numCompartments++; // Return the sealed capability which acts as a token of authentication for a component. - return compartments[numCompartments++].identifier; + return compartment_id; } /*