Post Scarcity
A prototype for a post scarcity programming environment
Loading...
Searching...
No Matches
vectorspace.c
Go to the documentation of this file.
1/*
2 * vectorspace.c
3 *
4 * Structures common to all vector space objects.
5 *
6 *
7 * (c) 2017 Simon Brooke <simon@journeyman.cc>
8 * Licensed under GPL version 2.0, or, at your option, any later version.
9 */
10
11#include <math.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <string.h>
15#include <stdio.h>
16/*
17 * wide characters
18 */
19#include <wchar.h>
20#include <wctype.h>
21
22#include "memory/conspage.h"
24#include "debug.h"
25#include "memory/hashmap.h"
26#include "memory/stack.h"
27#include "memory/vectorspace.h"
28#include "ops/intern.h"
29
30
31/**
32 * Make a cons_space_object which points to the vector_space_object
33 * with this `tag` at this `address`.
34 *
35 * @address the address of the vector_space_object to point to.
36 * @tag the vector-space tag of the particular type of vector-space object,
37 * NOT `VECTORPOINTTV`.
38 *
39 * @return a cons_pointer to the object, or NIL if the object could not be
40 * allocated due to memory exhaustion.
41 */
43 uint32_t tag ) {
44 debug_print( L"Entered make_vec_pointer\n", DEBUG_ALLOC );
45 struct cons_pointer pointer = allocate_cell( VECTORPOINTTV );
46 struct cons_space_object *cell = &pointer2cell( pointer );
47
49 L"make_vec_pointer: tag written, about to set pointer address to %p\n",
50 address );
51
52 cell->payload.vectorp.address = address;
53 cell->payload.vectorp.tag.value = tag;
54
56 L"make_vec_pointer: all good, returning pointer to %p\n",
57 cell->payload.vectorp.address );
58
60
61 return pointer;
62}
63
64/**
65 * Allocate a vector space object with this `payload_size` and `tag`,
66 * and return a `cons_pointer` which points to an object whigh points to it.
67 *
68 * @tag the vector-space tag of the particular type of vector-space object,
69 * NOT `VECTORPOINTTAG`.
70 * @payload_size the size of the payload required, in bytes.
71 *
72 * @return a cons_pointer to the object, or NIL if the object could not be
73 * allocated due to memory exhaustion.
74 */
75struct cons_pointer make_vso( uint32_t tag, uint64_t payload_size ) {
76 debug_print( L"Entered make_vso\n", DEBUG_ALLOC );
77 struct cons_pointer result = NIL;
78 int64_t total_size = sizeof( struct vector_space_header ) + payload_size;
79
80 /* Pad size to 64 bit words. This is intended to promote access efficiancy
81 * on 64 bit machines but may just be voodoo coding */
82 uint64_t padded = ceil( ( total_size * 8.0 ) / 8.0 );
83 debug_print( L"make_vso: about to malloc\n", DEBUG_ALLOC );
84 struct vector_space_object *vso = malloc( padded );
85
86 if ( vso != NULL ) {
87 memset( vso, 0, padded );
88 vso->header.tag.value = tag;
89
91 L"make_vso: written tag '%4.4s' into vso at %p\n",
92 vso->header.tag.bytes, vso );
93 result = make_vec_pointer( vso, tag );
95 vso->header.vecp = result;
96 // memcpy(vso->header.vecp, result, sizeof(struct cons_pointer));
97
98 vso->header.size = payload_size;
99
100#ifdef DEBUG
102 L"Allocated vector-space object of type %4.4s, total size %ld, payload size %ld, at address %p, payload address %p\n",
103 &vso->header.tag.bytes, total_size, vso->header.size,
104 vso, &vso->payload );
105 if ( padded != total_size ) {
106 debug_printf( DEBUG_ALLOC, L"\t\tPadded from %d to %d\n",
107 total_size, padded );
108 }
109#endif
110 }
111#ifdef DEBUG
113 L"make_vso: all good, returning pointer to %p\n",
114 pointer2cell( result ).payload.vectorp.address );
115#endif
116
117 return result;
118}
119
120/** for vector space pointers, free the actual vector-space
121 * object. Dangerous! */
122
123void free_vso( struct cons_pointer pointer ) {
124 struct cons_space_object cell = pointer2cell( pointer );
125
126 debug_printf( DEBUG_ALLOC, L"About to free vector-space object at 0x%lx\n",
127 cell.payload.vectorp.address );
128 struct vector_space_object *vso = cell.payload.vectorp.address;
129
130 switch ( vso->header.tag.value ) {
131 case HASHTV:
132 free_hashmap( pointer );
133 break;
134 case STACKFRAMETV:
135 free_stack_frame( get_stack_frame( pointer ) );
136 break;
137 }
138
139// free( (void *)cell.payload.vectorp.address );
140 debug_printf( DEBUG_ALLOC, L"Freed vector-space object at 0x%lx\n",
141 cell.payload.vectorp.address );
142}
143
144// bool check_vso_tag( struct cons_pointer pointer, char * tag) {
145// bool result = false;
146
147// if (check_tag(pointer, VECTORPOINTTAG)) {
148// struct vector_space_object * vso = pointer_to_vso(pointer);
149// result = strncmp( vso->header.tag.bytes[0], tag, TAGLENGTH);
150// }
151
152// return result;
153// }
struct cons_pointer allocate_cell(uint32_t tag)
Allocates a cell with the specified tag.
Definition conspage.c:222
#define VECTORPOINTTV
The string VECP, considered as an unsigned int.
union cons_space_object::@2 tag
union cons_space_object::@3 payload
#define NIL
a cons pointer which points to the special NIL cell
#define pointer2cell(pointer)
given a cons_pointer as argument, return the cell.
An indirect pointer to a cons cell.
an object in cons space.
void debug_dump_object(struct cons_pointer pointer, int level)
Like dump_object, q.v., but protected by the verbosity mechanism.
Definition debug.c:136
void debug_printf(int level, wchar_t *format,...)
wprintf adapted for the debug logging system.
Definition debug.c:101
void debug_print(wchar_t *message, int level)
print this debug message to stderr, if verbosity matches level.
Definition debug.c:41
#define DEBUG_ALLOC
Print messages debugging memory allocation.
Definition debug.h:21
void free_hashmap(struct cons_pointer pointer)
Free the hashmap indicated by this pointer.
Definition intern.c:110
struct stack_frame * get_stack_frame(struct cons_pointer pointer)
get the actual stackframe object from this pointer, or NULL if pointer is not a stackframe pointer.
Definition stack.c:53
void free_stack_frame(struct stack_frame *frame)
Free this stack frame.
Definition stack.c:224
#define STACKFRAMETV
Definition stack.h:31
struct cons_pointer make_vec_pointer(struct vector_space_object *address, uint32_t tag)
Make a cons_space_object which points to the vector_space_object with this tag at this address.
Definition vectorspace.c:42
void free_vso(struct cons_pointer pointer)
for vector space pointers, free the actual vector-space object.
struct cons_pointer make_vso(uint32_t tag, uint64_t payload_size)
Allocate a vector space object with this payload_size and tag, and return a cons_pointer which points...
Definition vectorspace.c:75
union vector_space_object::@5 payload
we'll malloc size bytes for payload, payload is just the first of these.
#define HASHTV
Definition vectorspace.h:30
struct vector_space_header header
the header of this object
struct cons_pointer vecp
back pointer to the vector pointer which uniquely points to this vso
Definition vectorspace.h:78
union vector_space_header::@4 tag
the tag (type) of this vector-space object.
uint64_t size
the size of my payload, in bytes
Definition vectorspace.h:80
the header which forms the start of every vector space object.
Definition vectorspace.h:69
a vector_space_object is just a vector_space_header followed by a lump of bytes; what we deem to be i...