Add attributes to functions, remove inline functions

For some reasons, I cannot compile correctly this library with inline
functions using Meson and Ninja (commit adding Meson build file
incoming).

Some attributes are also added, such as an attribute for all functions
receiving pointers as arguments telling these arguments cannot be null.
This commit is contained in:
Lucien Cartier-Tilet 2020-10-26 21:35:37 +01:00
parent fbed1b21d6
commit 988420a27c
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
3 changed files with 40 additions and 29 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.out *.out
vgcore* vgcore*
/build/

View File

@ -1,6 +1,11 @@
#include "vector.h" #include "vector.h"
static Result vec_warn_error(Result error) static void vec_maybe_destroy_element(Vector const *self,
size_t const t_index) NONNULL;
static Result vec_realloc(Vector *const self) NONNULL;
static Result vec_warn_error(Result error);
Result vec_warn_error(Result error)
{ {
PERR("%s", error.result.message); PERR("%s", error.result.message);
return error; return error;
@ -54,15 +59,15 @@ void *vec_safe_at(Vector const *const self, size_t const t_index)
return (t_index >= vec_length(self)) ? NULL : vec_at(self, t_index); return (t_index >= vec_length(self)) ? NULL : vec_at(self, t_index);
} }
static void vec_maybe_destroy_element(Vector const *self, size_t const t_index) void vec_maybe_destroy_element(Vector const *self, size_t const t_index)
{ {
void *element = vec_at(self, t_index);
if (self->destroy) { if (self->destroy) {
void *element = vec_at(self, t_index);
self->destroy(element); self->destroy(element);
} }
} }
static Result vec_realloc(Vector *const self) Result vec_realloc(Vector *const self)
{ {
self->capacity *= 2; self->capacity *= 2;
self->elements = realloc(self->elements, sizeof(void *) * vec_capacity(self)); self->elements = realloc(self->elements, sizeof(void *) * vec_capacity(self));
@ -117,3 +122,23 @@ void vec_delete(Vector *const self)
free(self->elements); free(self->elements);
free(self); free(self);
} }
size_t vec_length(Vector const *const self)
{
return self->length;
}
size_t vec_capacity(Vector const *const self)
{
return self->capacity;
}
void *vec_at(Vector const *const self, size_t const index)
{
return self->elements[index];
}
void *vec_last(Vector const *const self)
{
return vec_at(self, vec_length(self) - 1);
}

View File

@ -20,6 +20,7 @@
#define GRAY "\x1b[37m" #define GRAY "\x1b[37m"
#define UNINPLEMENTED printf("%s:%d: Not yet implemented", __FILE__, __LINE__) #define UNINPLEMENTED printf("%s:%d: Not yet implemented", __FILE__, __LINE__)
#define NONNULL __attribute__((nonnull))
#define INITIAL_CAPACITY 4 #define INITIAL_CAPACITY 4
#define PCOMMON(color, format, ...) \ #define PCOMMON(color, format, ...) \
fprintf(stderr, \ fprintf(stderr, \
@ -53,30 +54,14 @@ typedef struct Result_s {
Result vec_new(Destructor destructor); Result vec_new(Destructor destructor);
Result vec_with_capacity(Destructor const destructor, size_t const capacity); Result vec_with_capacity(Destructor const destructor, size_t const capacity);
Result vec_push(Vector *const self, void *const element); Result vec_push(Vector *const self, void *const element) NONNULL;
Result vec_shrink_to_fit(Vector *const self); Result vec_shrink_to_fit(Vector *const self) NONNULL;
void * vec_safe_at(Vector const *const self, size_t const index); void * vec_safe_at(Vector const *const self, size_t const index) NONNULL;
void vec_pop(Vector *const self); void * vec_at(Vector const *const self, size_t const index) NONNULL;
void vec_delete(Vector *const self); void * vec_last(Vector const *const self) NONNULL;
void vec_pop(Vector *const self) NONNULL;
inline size_t vec_length(Vector const *const self) void vec_delete(Vector *const self) NONNULL;
{ size_t vec_length(Vector const *const self) NONNULL;
return self->length; size_t vec_capacity(Vector const *const self) NONNULL;
}
inline size_t vec_capacity(Vector const *const self)
{
return self->capacity;
}
inline void *vec_at(Vector const *const self, size_t const index)
{
return self->elements[index];
}
inline void *vec_last(Vector const *const self)
{
return vec_at(self, vec_length(self) - 1);
}
#endif /* VECTOR_H */ #endif /* VECTOR_H */