Skip to content

Commit

Permalink
feat: 0.1.8 Improved Region API (#8)
Browse files Browse the repository at this point in the history
* feat: kls_push_zero() mallocs Region

* fix: kls_free() also frees kls->regs

* feat: add KLS_PUSH_NAMED(), KLS_PUSH_T_NAMED()

* feat: comment kls_push() from header file

* feat: define default Region name and desc

* feat: rework KLS_PRINTLIST()
  • Loading branch information
jgabaut committed Aug 24, 2023
1 parent b3784aa commit 7d1d8da
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 17 deletions.
1 change: 1 addition & 0 deletions bin/stego.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ tests# tests folder name
0.1.5# add kls_log(), fixes
0.1.6# functional Koliseo_Temp
0.1.7# add Region_List
0.1.8# improved Region API
4 changes: 4 additions & 0 deletions bin/v0.1.8/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#amboso compliant version folder, will ignore everything inside BUT the gitignore, to keep the clean dir
*
!.gitignore
!static
1 change: 1 addition & 0 deletions bin/v0.1.8/static
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Define the package name and version
AC_INIT([koliseo], [0.1.7], [jgabaut@github.com])
AC_INIT([koliseo], [0.1.8], [jgabaut@github.com])

# Verify automake version and enable foreign option
AM_INIT_AUTOMAKE([foreign -Wall])
Expand All @@ -24,7 +24,7 @@ fi
# Set a default version number if not specified externally
AC_ARG_VAR([VERSION], [Version number])
if test -z "$VERSION"; then
VERSION="0.1.7"
VERSION="0.1.8"
fi

# Output variables to the config.h header
Expand Down
2 changes: 1 addition & 1 deletion docs/koliseo.doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ PROJECT_NAME = "koliseo"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = "0.1.7"
PROJECT_NUMBER = "0.1.8"

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
79 changes: 72 additions & 7 deletions src/koliseo.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,61 @@ void* kls_push_zero(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t cou
memset(p, 0, size*count);
kls->prev_offset = kls->offset;
kls->offset += padding + size*count;
Region* reg = (Region*) malloc(sizeof(Region));
reg->begin_offset = kls->prev_offset;
reg->end_offset = kls->offset;
strcpy(reg->name, KOLISEO_DEFAULT_REGION_NAME);
strcpy(reg->desc,KOLISEO_DEFAULT_REGION_DESC);
Region_List reglist = kls_emptyList();
reglist = kls_cons(reg,reglist);
kls->regs = kls_append(reglist, kls->regs);

char msg[500];
sprintf(msg,"Pushed zeroes, size (%li) for KLS.",size);
kls_log("KLS",msg);
if (KOLISEO_DEBUG == 1) {
print_kls_2file(KOLISEO_DEBUG_FP,kls);
}
return p;
}

/**
* Takes a Koliseo pointer, and ptrdiff_t values for size, align and count. Tries pushing the specified amount of memory to the Koliseo data field, or goes to abort() if the operation fails.
* Uses the passed name and desc fields to initialise the allocated Region fields.
* Notably, it zeroes the memory region.
* @param kls The Koliseo at hand.
* @param size The size for data to push.
* @param align The alignment for data to push.
* @param count The multiplicative quantity to scale data size to push for.
* @return A void pointer to the start of memory just pushed to the Koliseo.
*/
void* kls_push_zero_named(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count, char* name, char* desc) {
ptrdiff_t available = kls->size - kls->offset;
ptrdiff_t padding = -kls->offset & (align -1);
if (count > PTRDIFF_MAX/size || (available - padding) < (size*count)) {
if (count > PTRDIFF_MAX/size) {
fprintf(stderr, "[KSL] count [%li] was bigger than PTRDIFF_MAX/size [%li].\n", count, PTRDIFF_MAX/size);
} else {
fprintf(stderr, "[KLS] Out of memory. size*count [%li] was bigger than available-padding [%li].\n", size*count, available-padding);
}
fprintf(stderr,"[KLS] Failed kls_push_zero() call.\n");
abort();
//return 0;
}
char* p = kls->data + kls->offset + padding;
//Zero new area
memset(p, 0, size*count);
kls->prev_offset = kls->offset;
kls->offset += padding + size*count;
Region* reg = (Region*) malloc(sizeof(Region));
reg->begin_offset = kls->prev_offset;
reg->end_offset = kls->offset;
strcpy(reg->name,name);
strcpy(reg->desc,desc);
Region_List reglist = kls_emptyList();
reglist = kls_cons(reg,reglist);
kls->regs = kls_append(reglist, kls->regs);

char msg[500];
sprintf(msg,"Pushed zeroes, size (%li) for KLS.",size);
kls_log("KLS",msg);
Expand Down Expand Up @@ -228,6 +283,7 @@ void kls_clear(Koliseo* kls) {
*/
void kls_free(Koliseo* kls) {
kls_clear(kls);
kls_freeList(kls->regs);
free(kls);
char msg[500];
sprintf(msg,"Freed KLS.");
Expand Down Expand Up @@ -319,19 +375,26 @@ void kls_freeList(Region_List l) {
else
{
kls_freeList(kls_tail(l));
kls_log("KLS","Freeing Region_List->value");
free(l->value);
kls_log("KLS","Freeing Region_List");
free(l);
}
return;
}

void kls_showList(Region_List l) {
void kls_showList_toFile(Region_List l, FILE* fp) {
if (fp == NULL) {
fprintf(stderr,"[KLS] kls_showList_toFile(): passed file was NULL.\n");
abort();
}
char msg[1000];
printf("[");
while (!kls_empty(l))
{
printf("--BEGIN Region--\n\n");
printf("Begin [%li] End [%li]\n",kls_head(l)->begin_offset,kls_head(l)->end_offset);
printf("Name [%s] Desc [%s]",kls_head(l)->name,kls_head(l)->desc);
fprintf(fp,"--BEGIN Region--\n\n");
fprintf(fp,"Begin [%li] End [%li]\n",kls_head(l)->begin_offset,kls_head(l)->end_offset);
fprintf(fp,"Name [%s] Desc [%s]",kls_head(l)->name,kls_head(l)->desc);
printf("\n\n--END Region--");
kls_log("KLS","--BEGIN Region--");
sprintf(msg,"Begin [%li] End [%li]",kls_head(l)->begin_offset,kls_head(l)->end_offset);
Expand All @@ -346,13 +409,15 @@ void kls_showList(Region_List l) {
l = kls_tail(l);
if (!kls_empty(l))
{
printf(",\n");
fprintf(fp,",\n");
}
}
printf("]\n");
fprintf(fp,"]\n");
}


void kls_showList(Region_List l) {
kls_showList_toFile(l,stdout);
}

bool kls_member(element el, Region_List l) {
if (kls_empty(l))
Expand Down
17 changes: 13 additions & 4 deletions src/koliseo.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#define KLS_MAJOR 0 /**< Represents current major release.*/
#define KLS_MINOR 1 /**< Represents current minor release.*/
#define KLS_PATCH 7 /**< Represents current patch release.*/
#define KLS_PATCH 8 /**< Represents current patch release.*/

/**
* Global variable for debug flag.
Expand All @@ -22,7 +22,7 @@ extern int KOLISEO_DEBUG;
*/
extern FILE* KOLISEO_DEBUG_FP;

static const char KOLISEO_API_VERSION_STRING[] = "0.1.7"; /**< Represents current version with MAJOR.MINOR.PATCH format.*/
static const char KOLISEO_API_VERSION_STRING[] = "0.1.8"; /**< Represents current version with MAJOR.MINOR.PATCH format.*/

const char* string_koliseo_version(void);

Expand All @@ -37,6 +37,7 @@ void kls_log(const char* tag, const char* msg);
/**
* Represents an allocated Region in a Koliseo.
* @see KLS_PUSH()
* @see KLS_PUSH_NAMED()
*/
typedef struct Region {
ptrdiff_t begin_offset; /**< Starting offset of memory region.*/
Expand All @@ -46,6 +47,9 @@ typedef struct Region {
char desc[255]; /**< Description field for the Region.*/
} Region;

static const char KOLISEO_DEFAULT_REGION_NAME[] = "No Name"; /**< Represents default Region name, used for kls_push_zero().*/
static const char KOLISEO_DEFAULT_REGION_DESC[] = "No Desc"; /**< Represents default Region desc, used for kls_push_zero().*/

#ifndef KOLISEO_LIST_H
#define KOLISEO_LIST_H
#include "stdbool.h"
Expand Down Expand Up @@ -92,11 +96,13 @@ ptrdiff_t kls_get_pos(Koliseo* kls);

Koliseo* kls_new(ptrdiff_t size);

void* kls_push(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
//void* kls_push(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
void* kls_push_zero(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);
void* kls_push_zero_named(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count, char* name, char* desc);
void* kls_pop(Koliseo* kls, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count);

#define KLS_PUSH(kls, type, count) (type*)kls_push_zero(kls, sizeof(type), _Alignof(type), count)
#define KLS_PUSH_NAMED(kls, type, count, name, desc) (type*)kls_push_zero_named(kls, sizeof(type), _Alignof(type), count, name, desc)
#define KLS_POP(kls, type, count) (type*)kls_pop(kls, sizeof(type), _Alignof(type), count)

#define KLS_PUSH_ARRAY(kls, type, count) (type*)kls_push_zero(kls, sizeof(type)*(count), _Alignof(type), count)
Expand All @@ -111,6 +117,7 @@ Koliseo_Temp kls_temp_start(Koliseo* kls);
void kls_temp_end(Koliseo_Temp tmp_kls);

#define KLS_PUSH_T(kls_temp, type, count) (type*)KLS_PUSH(kls_temp.kls, type, count)
#define KLS_PUSH_T_NAMED(kls_temp, type, count, name, desc) (type*)KLS_PUSH_NAMED(kls_temp.kls, type, count, name, desc)
#define KLS_POP_T(kls_temp, type, count) (type*)KLS_POP(kls_temp.kls, type, count)


Expand All @@ -124,7 +131,9 @@ Region_List kls_cons(element, Region_List);
void kls_freeList(Region_List);
#define KLS_FREELIST(kls_list) kls_freeList(kls_list)
void kls_showList(Region_List);
#define KLS_PRINTLIST(kls_list) kls_showList(kls_list)
void kls_showList_toFile(Region_List, FILE* fp);
#define KLS_ECHOLIST(kls_list) kls_showList(kls_list)
#define KLS_PRINTLIST(kls_list,file) kls_showList_toFile(kls_list,file)
bool kls_member(element, Region_List);
int kls_lenght(Region_List);
Region_List kls_append(Region_List, Region_List);
Expand Down
16 changes: 13 additions & 3 deletions static/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,39 @@ int main(void) {

printf("[Show Region list for Koliseo] [pos: %li]\n",kls_get_pos(kls));

KLS_PRINTLIST(kls->regs);
KLS_ECHOLIST(kls->regs);

Koliseo_Temp temp_kls = kls_temp_start(kls);
printf("[Started Koliseo_Temp] [pos: %li]\n",kls_get_pos(temp_kls.kls));

int minusone = -1;
int* p = &minusone;
int* p2 = &minusone;
int* p3 = &minusone;
printf("\n*p is [%i] before KLS_PUSH\n",*p);
printf("\n*p2 is [%i] before KLS_PUSH_T\n",*p2);
printf("\n*p3 is [%i] before KLS_PUSH_T\n",*p3);
printf("[KLS_PUSH for a int to Koliseo] [size: %li]\n",sizeof(int));
printf("[This handles the Koliseo directly while we have an open Koliseo_Temp.]\n");
p = (int*) KLS_PUSH(kls, int, 1);
printf("[KLS_PUSH_T_NAMED for a int to Koliseo_Temp] [size: %li]\n",sizeof(int));
p2 = (int*) KLS_PUSH_T_NAMED(temp_kls, int, 1,"int", "Another int");
printf("[KLS_PUSH_T for a int to Koliseo_Temp] [size: %li]\n",sizeof(int));
p2 = (int*) KLS_PUSH_T(temp_kls, int, 1);
p3 = (int*) KLS_PUSH_T(temp_kls, int, 1);
printf("[Current position in Koliseo] [pos: %li]\n",kls_get_pos(kls));
printf("[Current position in Koliseo_Temp] [pos: %li]\n",temp_kls.offset);
print_dbg_kls(kls);

*p = 1;
printf("\n*p is [%i] after KLS_PUSH\n",*p);
*p2 = 3;
*p2 = 2;
printf("\n*p2 is [%i] after KLS_PUSH\n",*p2);
*p2 = 3;
printf("\n*p3 is [%i] after KLS_PUSH\n",*p3);

printf("[Show reversed Region list for Koliseo] [pos: %li]\n",kls_get_pos(kls));

KLS_ECHOLIST(kls_reverse(kls->regs));

int* z = &minusone;
printf("\n*z is [%i] before KLS_POP\n",*z);
Expand Down

0 comments on commit 7d1d8da

Please sign in to comment.