Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const char *test_names[] = {
"da_append",
"sb_appendf",
"da_foreach",
"swap",
"temp_aligned_alloc",
};
#define test_names_count ARRAY_LEN(test_names)
Expand Down
13 changes: 13 additions & 0 deletions nob.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,18 @@ NOBDEF void nob_temp_reset(void);
NOBDEF size_t nob_temp_save(void);
NOBDEF void nob_temp_rewind(size_t checkpoint);

#define nob_swap(a, b) \
do { \
size_t sza = sizeof(a); \
NOB_ASSERT(sza == sizeof(b)); \
size_t checkpoint = nob_temp_save(); \
void *tmp = nob_temp_alloc(sza); \
memcpy(tmp, &a, sza); \
memcpy(&a, &b, sza); \
memcpy(&b, tmp, sza); \
nob_temp_rewind(checkpoint); \
} while(0)

// Given any path returns the last part of that path.
// "/path/to/a/file.c" -> "file.c"; "/path/to/a/directory" -> "directory"
NOBDEF const char *nob_path_name(const char *path);
Expand Down Expand Up @@ -2019,6 +2031,7 @@ NOBDEF int closedir(DIR *dirp)
#define sb_append_cstr nob_sb_append_cstr
#define sb_append_null nob_sb_append_null
#define sb_free nob_sb_free
#define swap nob_swap
#define Proc Nob_Proc
#define INVALID_PROC NOB_INVALID_PROC
#define Fd Nob_Fd
Expand Down
15 changes: 15 additions & 0 deletions tests/swap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#define NOB_IMPLEMENTATION
#define NOB_STRIP_PREFIX
#include "nob.h"

int main(void)
{
nob_log(INFO, "swap:");
int a = 10;
int b = 20;
nob_log(INFO, "a = %d, b = %d", a, b);
swap(a, b);
nob_log(INFO, "a = %d, b = %d", a, b);

return 0;
}