altos/scheme: Replace memory pool macros with inlines
authorKeith Packard <keithp@keithp.com>
Tue, 19 Dec 2017 20:16:24 +0000 (12:16 -0800)
committerKeith Packard <keithp@keithp.com>
Tue, 19 Dec 2017 20:16:24 +0000 (12:16 -0800)
AO_SCHEME_IS_CONST -> ao_scheme_is_const_addr
AO_SCHEME_IS_POOL -> ao_scheme_is_pool_addr

Provides better typechecking and avoids confusion with
ao_scheme_is_const inline (which takes an ao_poly instead of a void *)

Signed-off-by: Keith Packard <keithp@keithp.com>
src/scheme/ao_scheme.h
src/scheme/ao_scheme_frame.c
src/scheme/ao_scheme_mem.c
src/scheme/ao_scheme_poly.c

index b8e683fb0c83acd5a8e0744f3f2dc50b1ca48f8f..5cae0bda0558dc17f630be0e8dda60c9d58480ee 100644 (file)
@@ -155,8 +155,17 @@ ao_scheme_is_const(ao_poly poly) {
        return poly & AO_SCHEME_CONST;
 }
 
-#define AO_SCHEME_IS_CONST(a)  (ao_scheme_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_scheme_const + AO_SCHEME_POOL_CONST)
-#define AO_SCHEME_IS_POOL(a)   (ao_scheme_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_scheme_pool + AO_SCHEME_POOL)
+static inline int
+ao_scheme_is_const_addr(const void *addr) {
+       const uint8_t *a = addr;
+       return (ao_scheme_const <= a) && (a < ao_scheme_const + AO_SCHEME_POOL_CONST);
+}
+
+static inline int
+ao_scheme_is_pool_addr(const void *addr) {
+       const uint8_t *a = addr;
+       return (ao_scheme_pool <= a) && (a < ao_scheme_pool + AO_SCHEME_POOL);
+}
 
 void *
 ao_scheme_ref(ao_poly poly);
index 46f941e68cb7530b1891a6c7c731b178ba528ff2..a7e5153f5257f22d2594cdce596e94ed9bfcc62c 100644 (file)
@@ -86,8 +86,6 @@ frame_mark(void *addr)
                struct ao_scheme_frame_vals     *vals = ao_scheme_poly_frame_vals(frame->vals);
 
                MDBG_MOVE("frame mark %d\n", MDBG_OFFSET(frame));
-               if (!AO_SCHEME_IS_POOL(frame))
-                       break;
                if (!ao_scheme_mark_memory(&ao_scheme_frame_vals_type, vals))
                        frame_vals_mark(vals);
                frame = ao_scheme_poly_frame(frame->prev);
@@ -110,9 +108,6 @@ frame_move(void *addr)
                int                             ret;
 
                MDBG_MOVE("frame move %d\n", MDBG_OFFSET(frame));
-               if (!AO_SCHEME_IS_POOL(frame))
-                       break;
-
                vals = ao_scheme_poly_frame_vals(frame->vals);
                if (!ao_scheme_move_memory(&ao_scheme_frame_vals_type, (void **) &vals))
                        frame_vals_move(vals);
index a336fdfe964fccecf901405c0f0b05152a13538f..55872b6294ab3f246f0e1aa2f2033771fc073163 100644 (file)
@@ -313,7 +313,7 @@ static struct ao_scheme_chunk ao_scheme_chunk[AO_SCHEME_NCHUNK];
 /* Offset of an address within the pool. */
 static inline uint16_t pool_offset(void *addr) {
 #if DBG_MEM
-       if (!AO_SCHEME_IS_POOL(addr))
+       if (!ao_scheme_is_pool_addr(addr))
                ao_scheme_abort();
 #endif
        return ((uint8_t *) addr) - ao_scheme_pool;
@@ -723,7 +723,7 @@ ao_scheme_cons_check(struct ao_scheme_cons *cons)
        reset_chunks();
        walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref);
        while (cons) {
-               if (!AO_SCHEME_IS_POOL(cons))
+               if (!ao_scheme_is_pool_addr(cons))
                        break;
                offset = pool_offset(cons);
                if (busy(ao_scheme_busy, offset)) {
@@ -752,7 +752,7 @@ int
 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr)
 {
        int offset;
-       if (!AO_SCHEME_IS_POOL(addr))
+       if (!ao_scheme_is_pool_addr(addr))
                return 1;
 
        offset = pool_offset(addr);
@@ -804,7 +804,7 @@ ao_scheme_poly_mark(ao_poly p, uint8_t do_note_cons)
                return 1;
 
        addr = ao_scheme_ref(p);
-       if (!AO_SCHEME_IS_POOL(addr))
+       if (!ao_scheme_is_pool_addr(addr))
                return 1;
 
        if (type == AO_SCHEME_CONS && do_note_cons) {
@@ -864,7 +864,7 @@ ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref)
        void            *addr = *ref;
        uint16_t        offset, orig_offset;
 
-       if (!AO_SCHEME_IS_POOL(addr))
+       if (!ao_scheme_is_pool_addr(addr))
                return 1;
 
        (void) type;
@@ -874,7 +874,7 @@ ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref)
        offset = move_map(orig_offset);
        if (offset != orig_offset) {
                MDBG_MOVE("update ref %d %d -> %d\n",
-                         AO_SCHEME_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
+                         ao_scheme_is_pool_addr(ref) ? MDBG_OFFSET(ref) : -1,
                          orig_offset, offset);
                *ref = ao_scheme_pool + offset;
        }
@@ -914,7 +914,7 @@ ao_scheme_poly_move(ao_poly *ref, uint8_t do_note_cons)
                return 1;
 
        addr = ao_scheme_ref(p);
-       if (!AO_SCHEME_IS_POOL(addr))
+       if (!ao_scheme_is_pool_addr(addr))
                return 1;
 
        orig_offset = pool_offset(addr);
@@ -1060,7 +1060,7 @@ ao_scheme_print_mark_addr(void *addr)
                ao_scheme_abort();
 #endif
 
-       if (!AO_SCHEME_IS_POOL(addr))
+       if (!ao_scheme_is_pool_addr(addr))
                return 0;
 
        if (!ao_scheme_print_cleared) {
@@ -1084,7 +1084,7 @@ ao_scheme_print_clear_addr(void *addr)
                ao_scheme_abort();
 #endif
 
-       if (!AO_SCHEME_IS_POOL(addr))
+       if (!ao_scheme_is_pool_addr(addr))
                return;
 
        if (!ao_scheme_print_cleared)
index 25ac6d67d1eea6371e9f5cb7e9ad3828c4ee784e..0cffc19674db5428294a329adc401d13079afe0a 100644 (file)
@@ -65,7 +65,7 @@ ao_scheme_poly(const void *addr, ao_poly type) {
        const uint8_t   *a = addr;
        if (a == NULL)
                return AO_SCHEME_NIL;
-       if (AO_SCHEME_IS_CONST(a))
+       if (ao_scheme_is_const_addr(a))
                return AO_SCHEME_CONST | (a - ao_scheme_const + 4) | type;
        return (a - ao_scheme_pool + 4) | type;
 }