From f6ff40324926f919127c541f1af5ce772023c1c6 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Mon, 24 Dec 2018 15:12:17 +0000 Subject: [PATCH] Basics of vector space sort of done, but not yet working. --- Makefile | 2 +- src/conspage.c | 9 ++++++ src/consspaceobject.h | 13 ++++++-- src/vectorspace.c | 69 ++++++++++++++++++++++++++++++++++++++++++ src/vectorspace.h | 70 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 src/vectorspace.c create mode 100644 src/vectorspace.h diff --git a/Makefile b/Makefile index 4797c75..98a6bd3 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ INDENT_FLAGS := -nbad -bap -nbc -br -brf -brs -c33 -cd33 -ncdb -ce -ci4 -cli4 \ VERSION := "0.0.2" -CPPFLAGS ?= $(INC_FLAGS) -MMD -MP -g +CPPFLAGS ?= $(INC_FLAGS) -MMD -MP -g -DDEBUG LDFLAGS := -lm $(TARGET): $(OBJS) Makefile diff --git a/src/conspage.c b/src/conspage.c index 33e9828..8ba293b 100644 --- a/src/conspage.c +++ b/src/conspage.c @@ -159,6 +159,15 @@ void free_cell( struct cons_pointer pointer ) { case SYMBOLTV: dec_ref( cell->payload.string.cdr ); break; + case VECTORPOINTTV: + /* for vector space pointers, free the actual vector-space + * object. Dangerous! */ +#ifdef DEBUG + fwprintf(stderr, L"About to free vector-space object at %ld\n", cell->payload.vectorp.address); +#endif + free( (void *)cell->payload.vectorp.address); + break; + } if ( !check_tag( pointer, FREETAG ) ) { diff --git a/src/consspaceobject.h b/src/consspaceobject.h index de4d635..50ad5e1 100644 --- a/src/consspaceobject.h +++ b/src/consspaceobject.h @@ -127,12 +127,11 @@ * A pointer to an object in vector space. */ #define VECTORPOINTTAG "VECP" - +#define VECTORPOINTTV 0 /** * An open write stream. */ #define WRITETAG "WRIT" -/* TODO: this is wrong */ #define WRITETV 1414091351 /** @@ -222,7 +221,12 @@ * true if conspointer points to some sort of a number cell, * else false */ -#define numberp(conspoint) (check_tag(conspoint,INTEGERTAG)||check_tag(conspoint,REALTAG)) +#define numberp(conspoint) (check_tag(conspoint,INTEGERTAG)||check_tag(conspoint,RATIOTAG)||heck_tag(conspoint,REALTAG)) + +/** + * true if thr conspointer points to a vector pointer. + */ +#define vectorpointp(conspoint) (check_tag(conspoint,VECTORPOINTTAG)) /** * true if conspointer points to a write stream cell, else false. @@ -381,6 +385,9 @@ struct string_payload { struct cons_pointer cdr; }; +/** + * payload of a vector pointer cell. + */ struct vectorp_payload { union { char bytes[TAGLENGTH]; /* the tag (type) of the diff --git a/src/vectorspace.c b/src/vectorspace.c new file mode 100644 index 0000000..497838e --- /dev/null +++ b/src/vectorspace.c @@ -0,0 +1,69 @@ +/* + * vectorspace.c + * + * Structures common to all vector space objects. + * + * + * (c) 2017 Simon Brooke + * Licensed under GPL version 2.0, or, at your option, any later version. + */ + +#include +#include +#include +#include +/* + * wide characters + */ +#include +#include + +#include "conspage.h" +#include "consspaceobject.h" +#include "vectorspace.h" + + +/** + * make a cons-space object which points to the vector space object + * with this `tag` at this `address`. + * NOTE that `tag` should be the vector-space tag of the particular type of + * vector-space object, NOT `VECTORPOINTTAG`. + */ +struct cons_pointer make_vec_pointer(char *tag, uint64_t address) { + struct cons_pointer pointer = allocate_cell( VECTORPOINTTAG ); + struct cons_space_object cell = pointer2cell( pointer ); + + strncpy( &cell.payload.vectorp.tag.bytes[0], tag, 4 ); + cell.payload.vectorp.address = address; + + return pointer; +} + +/** + * allocate a vector space object with this `payload_size` and `tag`, + * and return a `cons_pointer` which points to an object whigh points to it. + * NOTE that `tag` should be the vector-space tag of the particular type of + * vector-space object, NOT `VECTORPOINTTAG`. + */ +struct cons_pointer make_vso( char *tag, long int payload_size) { + struct cons_pointer result = NIL; + long int total_size = sizeof(struct vector_space_header) + payload_size; + + struct vector_space_header *vso = malloc(total_size ); + + if (vso != NULL) { + strncpy( vso->tag.bytes[0], tag, TAGLENGTH ); + vso->vecp = make_vec_pointer(tag, (uint64_t)vso); + vso->size = payload_size; + +#ifdef DEBUG + fwprintf(stderr, L"Allocated vector-space object of type %s, total size %ld, payload size %ld\n", + tag, total_size, payload_size); +#endif + + result = vso->vecp; + } + + return result; +} + diff --git a/src/vectorspace.h b/src/vectorspace.h new file mode 100644 index 0000000..7fc90cc --- /dev/null +++ b/src/vectorspace.h @@ -0,0 +1,70 @@ +/** + * vectorspace.h + * + * Declarations common to all vector space objects. + * + * + * (c) 2017 Simon Brooke + * Licensed under GPL version 2.0, or, at your option, any later version. + */ + +#include +#include +#include +/* + * wide characters + */ +#include +#include + +#include "consspaceobject.h" + +#ifndef __vectorspace_h +#define __vectorspace_h + +/* + * part of the implementation structure of a namespace. + */ +#define HASHTAG "HASH" +#define HASHTV 0 + +/* + * a namespace (i.e. a binding of names to values, implemented as a hashmap) + */ +#define NAMESPACETAG "NMSP" +#define NAMESPACETV 0 + +/* + * a vector of cons pointers. + */ +#define VECTORTAG "VECT" +#define VECTORTV 0 + +#define pointer_to_vso(pointer)(vectorpointp(pointer)? pointer2cell(pointer).payload.vectorp.address : 0) + +struct cons_pointer make_vso( char *tag, long int payload_size); + +struct vector_space_header { + union { + char bytes[TAGLENGTH]; /* the tag (type) of the + * vector-space object this cell + * points to, considered as bytes. + * NOTE that the vector space object + * should itself have the identical + * tag. */ + uint32_t value; /* the tag considered as a number */ + } tag; + struct cons_pointer vecp; /* back pointer to the vector pointer + * which uniquely points to this vso */ + uint64_t size; /* the size of my payload, in bytes */ + char mark; /* mark bit for marking/sweeping the + * heap (not in this version) */ + char payload; /* we'll malloc `size` bytes for payload, + * `payload` is just the first of these. + * TODO: this is almost certainly not + * idiomatic C. */ +}; + +#endif + +