Skip to content

Commit 53c0568

Browse files
authored
Merge pull request #122 from saxbophone/develop
v0.20.0 - Cleanup and Improvements
2 parents d0e6d60 + 3c2c446 commit 53c0568

File tree

14 files changed

+243
-91
lines changed

14 files changed

+243
-91
lines changed

CMakeLists.txt

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# begin basic metadata
2424
cmake_minimum_required(VERSION 3.0)
2525

26-
project(libsaxbospiral VERSION 0.19.3 LANGUAGES C)
26+
project(libsaxbospiral VERSION 0.20.0 LANGUAGES C)
2727

2828
# set default C standard to use (C99)
2929
set(SAXBOSPIRAL_C_STANDARD "99")
@@ -50,7 +50,9 @@ if(CMAKE_SIZEOF_VOID_P LESS 8)
5050
message(
5151
WARNING
5252
"It looks like this system's architecture is not at least 64-bit.\n"
53-
"libsaxbospiral requires a system with an architecture of at least 64 bits!")
53+
"libsaxbospiral requires a system with an architecture of at least 64 bits!\n"
54+
"We'll continue trying to compile anyway, be sure to run the unit tests after."
55+
)
5456
endif()
5557

5658
# pass in version of library as preprocessor definitions
@@ -123,15 +125,37 @@ install(
123125
LIBRARY DESTINATION lib
124126
RUNTIME DESTINATION bin
125127
)
126-
# Install main library header files
128+
129+
# Generate rough (nearest major) version-dependent header installation folder
130+
set(
131+
LIBSAXBOSPIRAL_ROUGH_HEADER_DESTINATION
132+
"saxbospiral-${PROJECT_VERSION_MAJOR}"
133+
)
134+
# Generate precise (major and minor) version-dependent header installation folder
135+
set(
136+
LIBSAXBOSPIRAL_PRECISE_HEADER_DESTINATION
137+
"saxbospiral-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
138+
)
139+
140+
# Install main library header files, both to rough and precise install locations
141+
install(
142+
FILES ${LIB_SAXBOSPIRAL_HEADERS}
143+
DESTINATION "include/${LIBSAXBOSPIRAL_ROUGH_HEADER_DESTINATION}"
144+
)
145+
# Install render_backends header files
146+
install(
147+
FILES ${LIB_SAXBOSPIRAL_RENDER_BACKENDS_HEADERS}
148+
DESTINATION "include/${LIBSAXBOSPIRAL_ROUGH_HEADER_DESTINATION}/render_backends"
149+
)
150+
127151
install(
128152
FILES ${LIB_SAXBOSPIRAL_HEADERS}
129-
DESTINATION include/saxbospiral
153+
DESTINATION "include/${LIBSAXBOSPIRAL_PRECISE_HEADER_DESTINATION}"
130154
)
131155
# Install render_backends header files
132156
install(
133157
FILES ${LIB_SAXBOSPIRAL_RENDER_BACKENDS_HEADERS}
134-
DESTINATION include/saxbospiral/render_backends
158+
DESTINATION "include/${LIBSAXBOSPIRAL_PRECISE_HEADER_DESTINATION}/render_backends"
135159
)
136160

137161
enable_testing()

saxbospiral/initialise.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* You should have received a copy of the GNU Affero General Public License
2121
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
*/
23+
#include <assert.h>
2324
#include <stdint.h>
2425
#include <stdlib.h>
2526

@@ -54,8 +55,14 @@ sxbp_spiral_t sxbp_blank_spiral() {
5455
* this converts the 0s and 1s in the data into UP, LEFT, DOWN, RIGHT
5556
* instructions which are then used to build the pattern.
5657
* returns a status_t struct with error information (if needed)
58+
*
59+
* Asserts:
60+
* - That the spiral struct pointed to has its pointer attributes set to NULL
5761
*/
5862
sxbp_status_t sxbp_init_spiral(sxbp_buffer_t buffer, sxbp_spiral_t* spiral) {
63+
// preconditional assertions
64+
assert(spiral->lines == NULL);
65+
assert(spiral->co_ord_cache.co_ords.items == NULL);
5966
// result status object
6067
sxbp_status_t result;
6168
// number of lines is number of bits of the data, + 1 for the first UP line
@@ -67,8 +74,7 @@ sxbp_status_t sxbp_init_spiral(sxbp_buffer_t buffer, sxbp_spiral_t* spiral) {
6774
spiral->lines = calloc(sizeof(sxbp_line_t), line_count);
6875
// check for memory allocation failure
6976
if(spiral->lines == NULL) {
70-
result.location = SXBP_DEBUG;
71-
result.diagnostic = SXBP_MALLOC_REFUSED;
77+
result = SXBP_MALLOC_REFUSED;
7278
return result;
7379
}
7480
// First line is always an UP line - this is for orientation purposes
@@ -98,7 +104,7 @@ sxbp_status_t sxbp_init_spiral(sxbp_buffer_t buffer, sxbp_spiral_t* spiral) {
98104
}
99105
}
100106
// all ok
101-
result.diagnostic = SXBP_OPERATION_OK;
107+
result = SXBP_OPERATION_OK;
102108
return result;
103109
}
104110

saxbospiral/initialise.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ sxbp_spiral_t sxbp_blank_spiral();
4949
* this converts the 0s and 1s in the data into UP, LEFT, DOWN, RIGHT
5050
* instructions which are then used to build the pattern.
5151
* returns a status_t struct with error information (if needed)
52+
*
53+
* Asserts:
54+
* - That the spiral struct pointed to has its pointer attributes set to NULL
5255
*/
5356
sxbp_status_t sxbp_init_spiral(sxbp_buffer_t buffer, sxbp_spiral_t* spiral);
5457

saxbospiral/plot.c

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* You should have received a copy of the GNU Affero General Public License
2222
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
*/
24+
#include <assert.h>
2425
#include <stdlib.h>
2526

2627
#include "saxbospiral.h"
@@ -31,8 +32,18 @@
3132
extern "C"{
3233
#endif
3334

34-
// returns the sum of all line lengths within the given indexes
35+
/*
36+
* returns the sum of all line lengths within the given indexes
37+
*
38+
* Asserts:
39+
* - That start and end indexes are less than or equal to the spiral size
40+
* - That spiral.lines is not NULL
41+
*/
3542
size_t sxbp_sum_lines(sxbp_spiral_t spiral, size_t start, size_t end) {
43+
// preconditional assertions
44+
assert(start <= spiral.size);
45+
assert(end <= spiral.size);
46+
assert(spiral.lines != NULL);
3647
size_t size = 0;
3748
for(size_t i = start; i < end; i++) {
3849
size += spiral.lines[i].length;
@@ -49,22 +60,31 @@ size_t sxbp_sum_lines(sxbp_spiral_t spiral, size_t start, size_t end) {
4960
* each line segment is only one unit long, meaning multiple ones are needed for
5061
* lines longer than one unit.
5162
* returns a status struct with error information (if any)
63+
*
64+
* Asserts:
65+
* - That the output struct's items pointer is NULL
66+
* - That start and end indexes are less than or equal to the spiral size
67+
* - That spiral.lines is not NULL
5268
*/
5369
sxbp_status_t sxbp_spiral_points(
5470
sxbp_spiral_t spiral, sxbp_co_ord_array_t* output,
5571
sxbp_co_ord_t start_point, size_t start, size_t end
5672
) {
73+
// preconditional assertions
74+
assert(output->items == NULL);
75+
assert(start <= spiral.size);
76+
assert(end <= spiral.size);
77+
assert(spiral.lines != NULL);
5778
// prepare result status
58-
sxbp_status_t result = {{0, 0, 0}, 0};
79+
sxbp_status_t result;
5980
// the amount of space needed is the sum of all line lengths + 1 for end
6081
size_t size = sxbp_sum_lines(spiral, start, end) + 1;
6182
// allocate memory
6283
output->items = calloc(sizeof(sxbp_co_ord_t), size);
6384
// catch malloc error
6485
if(output->items == NULL) {
6586
// set error information then early return
66-
result.location = SXBP_DEBUG;
67-
result.diagnostic = SXBP_MALLOC_REFUSED;
87+
result = SXBP_MALLOC_REFUSED;
6888
return result;
6989
}
7090
output->size = size;
@@ -86,22 +106,29 @@ sxbp_status_t sxbp_spiral_points(
86106
}
87107
}
88108
// all good
89-
result.diagnostic = SXBP_OPERATION_OK;
109+
result = SXBP_OPERATION_OK;
90110
return result;
91111
}
92112

93113
/*
94-
* given a pointer to a spiral struct an limit, which is the index of the last
114+
* given a pointer to a spiral struct and limit, which is the index of the last
95115
* line to use, calculate and store the co-ordinates of all line segments that
96116
* would make up the spiral if the current lengths and directions were used.
97117
* each line segment is only one unit long, meaning multiple ones are needed for
98118
* lines longer than one unit. The co-ords are stored in the spiral's
99119
* co_ord_cache member and are re-used if they are still valid
100120
* returns a status struct with error information (if any)
121+
*
122+
* Asserts:
123+
* - That spiral->lines is not NULL
124+
* - That limit is less than or equal to spiral->size
101125
*/
102126
sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit) {
127+
// preconditional assertions
128+
assert(spiral->lines != NULL);
129+
assert(limit <= spiral->size);
103130
// prepare result status
104-
sxbp_status_t result = {{0, 0, 0}, 0};
131+
sxbp_status_t result;
105132
// the amount of space needed is the sum of all line lengths + 1 for end
106133
size_t size = sxbp_sum_lines(*spiral, 0, limit) + 1;
107134
// allocate / reallocate memory
@@ -122,8 +149,7 @@ sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit) {
122149
// catch malloc failure
123150
if(spiral->co_ord_cache.co_ords.items == NULL) {
124151
// set error information then early return
125-
result.location = SXBP_DEBUG;
126-
result.diagnostic = SXBP_MALLOC_REFUSED;
152+
result = SXBP_MALLOC_REFUSED;
127153
return result;
128154
}
129155
spiral->co_ord_cache.co_ords.size = size;
@@ -152,7 +178,7 @@ sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit) {
152178
*spiral, &missing, current, smallest, limit
153179
);
154180
// return errors from previous call if needed
155-
if(calculate_result.diagnostic != SXBP_OPERATION_OK) {
181+
if(calculate_result != SXBP_OPERATION_OK) {
156182
return calculate_result;
157183
}
158184
// add the missing co-ords to the cache
@@ -168,7 +194,7 @@ sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit) {
168194
limit > spiral->co_ord_cache.validity
169195
) ? limit : spiral->co_ord_cache.validity;
170196
// return ok
171-
result.diagnostic = SXBP_OPERATION_OK;
197+
result = SXBP_OPERATION_OK;
172198
return result;
173199
}
174200

saxbospiral/plot.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@
3333
extern "C"{
3434
#endif
3535

36-
// returns the sum of all line lengths within the given indexes
36+
/*
37+
* returns the sum of all line lengths within the given indexes
38+
*
39+
* Asserts:
40+
* - That start and end indexes are less than or equal to the spiral size
41+
* - That spiral.lines is not NULL
42+
*/
3743
size_t sxbp_sum_lines(sxbp_spiral_t spiral, size_t start, size_t end);
3844

3945
/*
@@ -45,20 +51,29 @@ size_t sxbp_sum_lines(sxbp_spiral_t spiral, size_t start, size_t end);
4551
* each line segment is only one unit long, meaning multiple ones are needed for
4652
* lines longer than one unit.
4753
* returns a status struct with error information (if any)
54+
*
55+
* Asserts:
56+
* - That the output struct's items pointer is NULL
57+
* - That start and end indexes are less than or equal to the spiral size
58+
* - That spiral.lines is not NULL
4859
*/
4960
sxbp_status_t sxbp_spiral_points(
5061
sxbp_spiral_t spiral, sxbp_co_ord_array_t* output,
5162
sxbp_co_ord_t start_point, size_t start, size_t end
5263
);
5364

5465
/*
55-
* given a pointer to a spiral struct an limit, which is the index of the last
66+
* given a pointer to a spiral struct and limit, which is the index of the last
5667
* line to use, calculate and store the co-ordinates of all line segments that
5768
* would make up the spiral if the current lengths and directions were used.
5869
* each line segment is only one unit long, meaning multiple ones are needed for
59-
* lines longer than one unit. The co-ords are stored in the
60-
* spiral's co_ord_cache member and are re-used if they are still valid
70+
* lines longer than one unit. The co-ords are stored in the spiral's
71+
* co_ord_cache member and are re-used if they are still valid
6172
* returns a status struct with error information (if any)
73+
*
74+
* Asserts:
75+
* - That spiral->lines is not NULL
76+
* - That limit is less than or equal to spiral->size
6277
*/
6378
sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit);
6479

saxbospiral/render.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* You should have received a copy of the GNU Affero General Public License
2121
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
*/
23+
#include <assert.h>
2324
#include <stdbool.h>
2425
#include <stdint.h>
2526
#include <stdlib.h>
@@ -39,8 +40,13 @@ extern "C"{
3940
* corners of the square needed to contain the points.
4041
* NOTE: This should NEVER be called with a pointer to anything other than a
4142
* 2-item struct of type co_ord_t
43+
*
44+
* Asserts:
45+
* - That spiral.co_ord_cache.co_ords.items is not NULL
4246
*/
4347
static void get_bounds(sxbp_spiral_t spiral, sxbp_co_ord_t* bounds) {
48+
// preconditional assertions
49+
assert(spiral.co_ord_cache.co_ords.items != NULL);
4450
sxbp_tuple_item_t min_x = 0;
4551
sxbp_tuple_item_t min_y = 0;
4652
sxbp_tuple_item_t max_x = 0;
@@ -70,10 +76,17 @@ static void get_bounds(sxbp_spiral_t spiral, sxbp_co_ord_t* bounds) {
7076
* given a spiral struct and a pointer to a blank bitmap_t struct, writes data
7177
* representing a monochromatic image of the rendered spiral to the bitmap
7278
* returns a status struct with error information (if any)
79+
*
80+
* Asserts:
81+
* - That image->pixels is NULL
82+
* - That spiral.lines is not NULL
7383
*/
7484
sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image) {
85+
// preconditional assertions
86+
assert(image->pixels == NULL);
87+
assert(spiral.lines != NULL);
7588
// create result status struct
76-
sxbp_status_t result = {{0, 0, 0}, 0};
89+
sxbp_status_t result;
7790
// plot co-ords of spiral into it's cache
7891
sxbp_cache_spiral_points(&spiral, spiral.size);
7992
// get the min and max bounds of the spiral's co-ords
@@ -96,8 +109,7 @@ sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image) {
96109
image->pixels = malloc(image->width * sizeof(bool*));
97110
// check for malloc fail
98111
if(image->pixels == NULL) {
99-
result.location = SXBP_DEBUG;
100-
result.diagnostic = SXBP_MALLOC_REFUSED;
112+
result = SXBP_MALLOC_REFUSED;
101113
return result;
102114
}
103115
for(size_t i = 0; i < image->width; i++) {
@@ -108,8 +120,9 @@ sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image) {
108120
for(size_t j = i; j > 0; j--) {
109121
free(image->pixels[j]);
110122
}
111-
result.location = SXBP_DEBUG;
112-
result.diagnostic = SXBP_MALLOC_REFUSED;
123+
// now we need to free() the top-level array
124+
free(image->pixels);
125+
result = SXBP_MALLOC_REFUSED;
113126
return result;
114127
}
115128
}
@@ -140,7 +153,7 @@ sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image) {
140153
}
141154
}
142155
// status ok
143-
result.diagnostic = SXBP_OPERATION_OK;
156+
result = SXBP_OPERATION_OK;
144157
return result;
145158
}
146159

saxbospiral/render.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ typedef struct sxbp_bitmap_t {
4343
* given a spiral struct and a pointer to a blank bitmap_t struct, writes data
4444
* representing a monochromatic image of the rendered spiral to the bitmap
4545
* returns a status struct with error information (if any)
46+
*
47+
* Asserts:
48+
* - That image->pixels is NULL
49+
* - That spiral.lines is not NULL
4650
*/
4751
sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image);
4852

0 commit comments

Comments
 (0)