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
18/*
19 * wide characters
20 */
21#include <wchar.h>
22#include <wctype.h>
23
24#include "memory/conspage.h"
26#include "debug.h"
27#include "io/io.h"
28#include "memory/hashmap.h"
29#include "memory/stack.h"
30#include "memory/vectorspace.h"
31#include "ops/intern.h"
32
33
34/**
35 * Make a cons_space_object which points to the vector_space_object
36 * with this `tag` at this `address`.
37 *
38 * @address the address of the vector_space_object to point to.
39 * @tag the vector-space tag of the particular type of vector-space object,
40 * NOT `VECTORPOINTTV`.
41 *
42 * @return a cons_pointer to the object, or NIL if the object could not be
43 * allocated due to memory exhaustion.
44 */
46 uint32_t tag ) {
47 debug_print( L"Entered make_vec_pointer\n", DEBUG_ALLOC );
48 struct cons_pointer pointer = allocate_cell( VECTORPOINTTV );
49 struct cons_space_object *cell = &pointer2cell( pointer );
50
52 L"make_vec_pointer: tag written, about to set pointer address to %p\n",
53 address );
54
55 cell->payload.vectorp.address = address;
56 cell->payload.vectorp.tag.value = tag;
57
59 L"make_vec_pointer: all good, returning pointer to %p\n",
60 cell->payload.vectorp.address );
61
63
64 return pointer;
65}
66
67/**
68 * Allocate a vector space object with this `payload_size` and `tag`,
69 * and return a `cons_pointer` which points to an object whigh points to it.
70 *
71 * @tag the vector-space tag of the particular type of vector-space object,
72 * NOT `VECTORPOINTTAG`.
73 * @payload_size the size of the payload required, in bytes.
74 *
75 * @return a cons_pointer to the object, or NIL if the object could not be
76 * allocated due to memory exhaustion.
77 */
78struct cons_pointer make_vso( uint32_t tag, uint64_t payload_size ) {
79 debug_print( L"Entered make_vso\n", DEBUG_ALLOC );
80 struct cons_pointer result = NIL;
81 int64_t total_size = sizeof( struct vector_space_header ) + payload_size;
82
83 /* Pad size to 64 bit words. This is intended to promote access efficiancy
84 * on 64 bit machines but may just be voodoo coding */
85 uint64_t padded = ceil( ( total_size * 8.0 ) / 8.0 );
86 debug_print( L"make_vso: about to malloc\n", DEBUG_ALLOC );
87 struct vector_space_object *vso = malloc( padded );
88
89 if ( vso != NULL ) {
90 memset( vso, 0, padded );
91 vso->header.tag.value = tag;
92
94 L"make_vso: written tag '%4.4s' into vso at %p\n",
95 vso->header.tag.bytes, vso );
96 result = make_vec_pointer( vso, tag );
98 vso->header.vecp = result;
99 // memcpy(vso->header.vecp, result, sizeof(struct cons_pointer));
100
101 vso->header.size = payload_size;
102
103#ifdef DEBUG
105 L"Allocated vector-space object of type %4.4s, total size %ld, payload size %ld, at address %p, payload address %p\n",
106 &vso->header.tag.bytes, total_size, vso->header.size,
107 vso, &vso->payload );
108 if ( padded != total_size ) {
109 debug_printf( DEBUG_ALLOC, L"\t\tPadded from %d to %d\n",
110 total_size, padded );
111 }
112#endif
113 }
114#ifdef DEBUG
116 L"make_vso: all good, returning pointer to %p\n",
117 pointer2cell( result ).payload.vectorp.address );
118#endif
119
120 return result;
121}
122
123/** for vector space pointers, free the actual vector-space
124 * object. Dangerous! */
125
126void free_vso( struct cons_pointer pointer ) {
127 struct cons_space_object cell = pointer2cell( pointer );
128
130 L"About to free vector-space object of type %s at 0x%lx\n",
131 ( char * ) cell.payload.vectorp.tag.bytes,
132 cell.payload.vectorp.address );
133 struct vector_space_object *vso = cell.payload.vectorp.address;
134
135 switch ( vso->header.tag.value ) {
136 case HASHTV:
137 free_hashmap( pointer );
138 break;
139 case STACKFRAMETV:
140 free_stack_frame( get_stack_frame( pointer ) );
141 break;
142 }
143
144// free( (void *)cell.payload.vectorp.address );
145 debug_printf( DEBUG_ALLOC, L"Freed vector-space object at 0x%lx\n",
146 cell.payload.vectorp.address );
147}
148
149// bool check_vso_tag( struct cons_pointer pointer, char * tag) {
150// bool result = false;
151
152// if (check_tag(pointer, VECTORPOINTTAG)) {
153// struct vector_space_object * vso = pointer_to_vso(pointer);
154// result = strncmp( vso->header.tag.bytes[0], tag, TAGLENGTH);
155// }
156
157// return result;
158// }
struct cons_pointer allocate_cell(uint32_t tag)
Allocates a cell with the specified tag.
Definition conspage.c:231
#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:24
void free_hashmap(struct cons_pointer pointer)
Free the hashmap indicated by this pointer.
Definition intern.c:111
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:222
#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:45
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:78
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...