Compare commits
2 Commits
5a84ca7d8d
...
520e745a97
Author | SHA1 | Date |
---|---|---|
falsycat | 520e745a97 | |
falsycat | cb9e6439c5 |
|
@ -1,6 +1,6 @@
|
|||
#!/bin/bash
|
||||
|
||||
tests=$(cat $@ | sed -e '/^NF7_TEST(/!d; s|^NF7_TEST(\([^)]*\)).*$|\1|' | xargs echo)
|
||||
tests=$(cat ${@//;/ } | sed -e '/^NF7_TEST(/!d; s|^NF7_TEST(\([^)]*\)).*$|\1|' | xargs echo)
|
||||
|
||||
echo "#include \"test/common.h\""
|
||||
echo "#include \"test/run.h\""
|
||||
|
|
|
@ -6,10 +6,12 @@ target_sources(nf7util
|
|||
array.h
|
||||
log.h
|
||||
malloc.h
|
||||
refcnt.h
|
||||
signal.h
|
||||
)
|
||||
target_tests(nf7util
|
||||
array.test.c
|
||||
refcnt.test.c
|
||||
)
|
||||
target_link_libraries(nf7util
|
||||
PRIVATE
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
// No copyright
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "util/malloc.h"
|
||||
|
||||
|
||||
#define NF7_REFCNT_DECL(ATTR, T) \
|
||||
ATTR void T##_ref(struct T*); \
|
||||
ATTR bool T##_unref(struct T*); \
|
||||
static_assert(true)
|
||||
|
||||
|
||||
#define NF7_REFCNT_IMPL(ATTR, T, DELETER) \
|
||||
ATTR void T##_ref(struct T* this) { \
|
||||
++this->refcnt; \
|
||||
} \
|
||||
ATTR bool T##_unref(struct T* this) { \
|
||||
assert(0 < this->refcnt); \
|
||||
if (0 == --this->refcnt) { \
|
||||
{DELETER}; \
|
||||
return true; \
|
||||
} \
|
||||
return false; \
|
||||
} \
|
||||
static_assert(true)
|
|
@ -0,0 +1,25 @@
|
|||
// No copyright
|
||||
#include "util/refcnt.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "test/common.h"
|
||||
|
||||
|
||||
struct mystruct {
|
||||
bool deleted;
|
||||
uint64_t refcnt;
|
||||
};
|
||||
NF7_REFCNT_IMPL(static inline, mystruct, {this->deleted = true;});
|
||||
|
||||
|
||||
NF7_TEST(nf7_util_refcnt_test_delete) {
|
||||
struct mystruct sut = {0};
|
||||
mystruct_ref(&sut);
|
||||
mystruct_ref(&sut);
|
||||
return
|
||||
nf7_test_expect(!mystruct_unref(&sut)) &&
|
||||
nf7_test_expect(!sut.deleted) &&
|
||||
nf7_test_expect(mystruct_unref(&sut)) &&
|
||||
nf7_test_expect(sut.deleted);
|
||||
}
|
Loading…
Reference in New Issue