Skip to content

Commit

Permalink
chore: update to C23
Browse files Browse the repository at this point in the history
  • Loading branch information
laurelmay committed Sep 21, 2024
1 parent 6c320f2 commit cb0bbfa
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 66 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Kyle Laker
Copyright (c) 2024 Laurel May

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC=clang

override CFLAGS := -Wall -pedantic -std=c17 $(CFLAGS)
override CFLAGS := -Wall -pedantic -std=c23 $(CFLAGS)
override LDFLAGS := $(LDFLAGS)

SRCDIR = src
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ HEAD->link ^= TAIL->link

## Installing

### Dependencies

- `subunit`
- `libcheck`

A typical `make && sudo make install` will work on most Linux distros (though
be careful! `/usr/` will be modified). For ArchLinux users, a `PKGBUILD` is
provided. You can build with `makepkg -si` to install the latest released
Expand All @@ -87,4 +92,4 @@ version. This package is not currently available in the Arch repos or the AUR.
## Notes

Pointers are not integers. This very heavily treats pointers as if they were
in fact integers. That's a horrible idea.
in fact integers. That's a horrible idea.
2 changes: 1 addition & 1 deletion compile_flags.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-Iinclude
-Wall
--std=c17
--std=c23
-pedantic
4 changes: 2 additions & 2 deletions include/list.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file list.h
* @author Kyle Laker (kyle@laker.email)
* @author Laurel May (laurel@laurelmay.me)
*
* @copyright Copyright (c) 2022
*
Expand Down Expand Up @@ -66,7 +66,7 @@ typedef struct
typedef void (*element_destructor)(list_val_t);

/* Exported list functions */
list_t *list_create();
list_t *list_create(void);
void list_destroy(list_t *, element_destructor);
int list_insert(list_t *, size_t, list_val_t);
int list_append(list_t *, list_val_t);
Expand Down
56 changes: 28 additions & 28 deletions src/xorlist.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @internal
* @file xorlist.c
* @author Kyle Laker (kyle@laker.email)
* @author Laurel May (laurel@laurelmay.me)
*
* @copyright Copyright (c) 2022
*
Expand All @@ -13,8 +13,8 @@
#include "list.h"

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

Expand Down Expand Up @@ -55,17 +55,17 @@ static node_pair_t traverse_to_idx(list_t *, size_t);
* returned list should not be passed to free directly and should be passed
* to list_destroy(list_t *, element_destructor).
*/
list_t *list_create() {
list_t *list_create(void) {
list_t *list = malloc(sizeof(list_t));
node_t *head = malloc(sizeof(node_t));
node_t *tail = malloc(sizeof(node_t));

if (!list || !head || !tail) {
return NULL;
return nullptr;
}

head->link = calc_new_ptr(NULL, NULL, tail);
tail->link = calc_new_ptr(head, NULL, NULL);
head->link = calc_new_ptr(nullptr, nullptr, tail);
tail->link = calc_new_ptr(head, nullptr, nullptr);

list->head = head;
list->tail = tail;
Expand All @@ -85,7 +85,7 @@ list_t *list_create() {
* \ref list_val_t as an argument (for example, `free(void *)`).
*
* @param list The list to tear down
* @param destroy A function to properly free list elements (or `NULL`)
* @param destroy A function to properly free list elements (or `nullptr`)
*/
void list_destroy(list_t *list, element_destructor destroy) {
/* Destroy all remaining items in the list. */
Expand All @@ -99,8 +99,8 @@ void list_destroy(list_t *list, element_destructor destroy) {
/* Free memory for list struct members. */
free(list->head);
free(list->tail);
list->head = NULL;
list->tail = NULL;
list->head = nullptr;
list->tail = nullptr;
free(list);
}

Expand Down Expand Up @@ -138,7 +138,7 @@ int list_insert(list_t *list, size_t idx, list_val_t value) {
* @return int A non-zero value on failure
*/
int list_append(list_t *list, list_val_t value) {
return add_at_node(list, value, list_prev(list->tail, NULL), list->tail);
return add_at_node(list, value, list_prev(list->tail, nullptr), list->tail);
}

/**
Expand All @@ -162,7 +162,7 @@ int list_enqueue(list_t *list, list_val_t value) {
* @return int A non-zero value on failure
*/
int list_prepend(list_t *list, list_val_t value) {
return add_at_node(list, value, list->head, list_next(list->head, NULL));
return add_at_node(list, value, list->head, list_next(list->head, nullptr));
}

/**
Expand Down Expand Up @@ -196,12 +196,12 @@ bool list_is_empty(list_t list) {
*
* @param list The list to remove from
* @param idx The index to remove at
* @return list_val_t The item at that index (or NULL for an invalid index)
* @return list_val_t The item at that index (or nullptr for an invalid index)
*/
list_val_t list_delete(list_t *list, size_t idx) {
node_pair_t nodes = traverse_to_idx(list, idx);
if (!nodes.prev || !nodes.curr) {
return NULL;
return nullptr;
}

node_t *prev = nodes.prev;
Expand All @@ -216,7 +216,7 @@ list_val_t list_delete(list_t *list, size_t idx) {
list_val_t val = curr->value;

free(curr);
curr = NULL;
curr = nullptr;
list->size -= 1;

return val;
Expand All @@ -228,7 +228,7 @@ list_val_t list_delete(list_t *list, size_t idx) {
* This is equivalent to list_delete(list_t *, size_t) with an index of 0.
*
* @param list The list to remove from
* @return list_val_t The item at the top of the stack (or NULL if empty)
* @return list_val_t The item at the top of the stack (or nullptr if empty)
*/
list_val_t list_pop(list_t *list) {
return list_delete(list, 0);
Expand All @@ -240,7 +240,7 @@ list_val_t list_pop(list_t *list) {
* This is equivalent to list_delete(list_t *, size_t) with an index of 0.
*
* @param list The queue to remove from
* @return list_val_t The first item in the queue (or NULL if empty)
* @return list_val_t The first item in the queue (or nullptr if empty)
*/
list_val_t list_dequeue(list_t *list) {
return list_delete(list, 0);
Expand All @@ -255,12 +255,12 @@ list_val_t list_dequeue(list_t *list) {
*/
list_val_t list_get(list_t list, size_t idx) {
if (idx >= list.size) {
return NULL;
return nullptr;
}

node_pair_t nodes = traverse_to_idx(&list, idx);
if (!nodes.prev || !nodes.curr) {
return NULL;
return nullptr;
}

return nodes.curr->value;
Expand All @@ -271,7 +271,7 @@ list_val_t list_get(list_t list, size_t idx) {
*
* @param list The stack/queue to peek at
* @return list_val_t The item at the front of queue (or top of the stack) or
* NULL if empty
* nullptr if empty
*/
list_val_t list_peek(list_t list) {
return list_get(list, 0);
Expand All @@ -286,13 +286,13 @@ list_val_t list_peek(list_t list) {
* @param list The list to modify
* @param idx The index to modify
* @param value The new value to set
* @return list_val_t The previous at the given index (or NULL if the index
* @return list_val_t The previous at the given index (or nullptr if the index
* is invalid)
*/
list_val_t list_set(list_t *list, size_t idx, list_val_t value) {
node_pair_t nodes = traverse_to_idx(list, idx);
if (!nodes.curr) {
return NULL;
return nullptr;
}

list_val_t prior_value = nodes.curr->value;
Expand Down Expand Up @@ -362,7 +362,7 @@ void list_reverse(list_t *list) {
* @return ssize_t The index of `value` or -1 if not found
*/
ssize_t list_find(list_t list, list_val_t value) {
node_t *curr = list_next(list.head, NULL);
node_t *curr = list_next(list.head, nullptr);
node_t *prev = list.head;
size_t idx = 0;

Expand Down Expand Up @@ -394,7 +394,7 @@ ssize_t list_find(list_t list, list_val_t value) {
*/
bool list_contains(list_t list, list_val_t value) {
node_t *curr = list.head;
node_t *prev = NULL;
node_t *prev = nullptr;

while (curr != list.tail) {
node_t *next_node = list_next(curr, prev);
Expand Down Expand Up @@ -424,14 +424,14 @@ static node_t *calc_new_ptr(void *a, void *b, void *c) {
* Returns the next item in the list after curr.
*/
static node_t *list_next(node_t *curr, node_t *prev) {
return calc_new_ptr(prev, curr->link, NULL);
return calc_new_ptr(prev, curr->link, nullptr);
}

/**
* Returns the previous item in the list before curr.
*/
static node_t *list_prev(node_t *curr, node_t *next) {
return calc_new_ptr(NULL, curr->link, next);
return calc_new_ptr(nullptr, curr->link, next);
}

/**
Expand All @@ -447,7 +447,7 @@ static int add_at_node(list_t *list, list_val_t value, node_t *before, node_t *a
new_node->value = value;

/* Set the pointers to surrounding nodes. */
new_node->link = calc_new_ptr(before, NULL, after);
new_node->link = calc_new_ptr(before, nullptr, after);
after->link = calc_new_ptr(before, new_node, after->link);
before->link = calc_new_ptr(before->link, new_node, after);

Expand All @@ -468,7 +468,7 @@ static node_pair_t traverse_to_idx(list_t *list, size_t idx) {
* If the index isn't valid, don't bother searching at all.
*/
if (idx > list->size) {
node_pair_t all_null = {.prev = NULL, .curr = NULL};
node_pair_t all_null = {.prev = nullptr, .curr = nullptr};
return all_null;
}

Expand All @@ -489,7 +489,7 @@ static node_pair_t traverse_to_idx(list_t *list, size_t idx) {
}

/* Start traversing from the end until the needed index. */
node_t *curr = list_next(starting_end, NULL);
node_t *curr = list_next(starting_end, nullptr);
node_t *prev = starting_end;

for (int i = 0; i < num_iter; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

CC=clang

override CFLAGS := -g -O0 -Wall -pedantic -std=c17 $(CFLAGS)
override CFLAGS := -g -O0 -Wall -pedantic -std=c23 $(CFLAGS)
override LDFLAGS := -g -O0 $(LDFLAGS)

SRCDIR=../src
Expand Down
Loading

0 comments on commit cb0bbfa

Please sign in to comment.