Basics of vector space sort of done, but not yet working.

This commit is contained in:
Simon Brooke 2018-12-24 15:12:17 +00:00
parent 7b1cdf4440
commit f6ff403249
5 changed files with 159 additions and 4 deletions

View file

@ -17,7 +17,7 @@ INDENT_FLAGS := -nbad -bap -nbc -br -brf -brs -c33 -cd33 -ncdb -ce -ci4 -cli4 \
VERSION := "0.0.2" VERSION := "0.0.2"
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP -g CPPFLAGS ?= $(INC_FLAGS) -MMD -MP -g -DDEBUG
LDFLAGS := -lm LDFLAGS := -lm
$(TARGET): $(OBJS) Makefile $(TARGET): $(OBJS) Makefile

View file

@ -159,6 +159,15 @@ void free_cell( struct cons_pointer pointer ) {
case SYMBOLTV: case SYMBOLTV:
dec_ref( cell->payload.string.cdr ); dec_ref( cell->payload.string.cdr );
break; 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 ) ) { if ( !check_tag( pointer, FREETAG ) ) {

View file

@ -127,12 +127,11 @@
* A pointer to an object in vector space. * A pointer to an object in vector space.
*/ */
#define VECTORPOINTTAG "VECP" #define VECTORPOINTTAG "VECP"
#define VECTORPOINTTV 0
/** /**
* An open write stream. * An open write stream.
*/ */
#define WRITETAG "WRIT" #define WRITETAG "WRIT"
/* TODO: this is wrong */
#define WRITETV 1414091351 #define WRITETV 1414091351
/** /**
@ -222,7 +221,12 @@
* true if conspointer points to some sort of a number cell, * true if conspointer points to some sort of a number cell,
* else false * 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. * true if conspointer points to a write stream cell, else false.
@ -381,6 +385,9 @@ struct string_payload {
struct cons_pointer cdr; struct cons_pointer cdr;
}; };
/**
* payload of a vector pointer cell.
*/
struct vectorp_payload { struct vectorp_payload {
union { union {
char bytes[TAGLENGTH]; /* the tag (type) of the char bytes[TAGLENGTH]; /* the tag (type) of the

69
src/vectorspace.c Normal file
View file

@ -0,0 +1,69 @@
/*
* vectorspace.c
*
* Structures common to all vector space objects.
*
*
* (c) 2017 Simon Brooke <simon@journeyman.cc>
* Licensed under GPL version 2.0, or, at your option, any later version.
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/*
* wide characters
*/
#include <wchar.h>
#include <wctype.h>
#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;
}

70
src/vectorspace.h Normal file
View file

@ -0,0 +1,70 @@
/**
* vectorspace.h
*
* Declarations common to all vector space objects.
*
*
* (c) 2017 Simon Brooke <simon@journeyman.cc>
* Licensed under GPL version 2.0, or, at your option, any later version.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
/*
* wide characters
*/
#include <wchar.h>
#include <wctype.h>
#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