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.
master
Lucien Cartier-Tilet 2 years ago
parent fbed1b21d6
commit 988420a27c
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
  1. 1
      .gitignore
  2. 33
      vector.c
  3. 35
      vector.h

1
.gitignore vendored

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

@ -1,6 +1,11 @@
#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);
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);
}
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) {
void *element = vec_at(self, t_index);
self->destroy(element);
}
}
static Result vec_realloc(Vector *const self)
Result vec_realloc(Vector *const self)
{
self->capacity *= 2;
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);
}
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);
}

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