-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce rudimentary object system to unify the number of different objects that we have Fixes #105
- Loading branch information
1 parent
4575c65
commit 1359d87
Showing
7 changed files
with
221 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
Copyright 2016 Vanderbilt University | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
#ifndef ACCRE_OBJECT_H_INCLUDED | ||
#define ACCRE_OBJECT_H_INCLUDED | ||
|
||
#include <tbx/ref.h> | ||
|
||
// Types | ||
typedef struct tbx_vtable_t tbx_vtable_t; | ||
typedef struct tbx_obj_t tbx_obj_t; | ||
|
||
// Exported types. OK to be exported | ||
/*! | ||
* A single instance of this exists per type. It MUST be the first element of | ||
* the type's vtable to allow type punning to work. | ||
*/ | ||
struct tbx_vtable_t { | ||
/*! Function to be called when refcount reaches zero */ | ||
tbx_ref_release_fn_t free_fn; | ||
/*! Human printable name of this type */ | ||
char *name; | ||
}; | ||
|
||
/*! | ||
* Each instance contains this object. It MUST be the first element of the | ||
* object to allow type punning for opaque types | ||
*/ | ||
struct tbx_obj_t { | ||
const tbx_vtable_t *vtable; | ||
tbx_ref_t refcount; | ||
}; | ||
|
||
// Inline functions | ||
/*! | ||
* @brief Properly initializes reference count | ||
* @param obj Object to initialize | ||
* @param vtable The desired vtable for the object to use | ||
* This function is necessary because any cross-CPU caches need to be atomically | ||
* updated to be notified. Otherwise cache coherency can act up | ||
*/ | ||
static inline void tbx_obj_init(tbx_obj_t *obj, const tbx_vtable_t *vtable) { | ||
tbx_ref_init(&obj->refcount); | ||
obj->vtable = vtable; | ||
} | ||
|
||
/*! | ||
* @brief Grabs a new reference incrementing the reference count | ||
* @param ref Refcount to increment | ||
* @returns Pointer to refcount | ||
*/ | ||
static inline tbx_obj_t *tbx_obj_get(tbx_obj_t *obj) { | ||
tbx_ref_get(&obj->refcount); | ||
return obj; | ||
} | ||
|
||
/*! | ||
* @brief Decrements a reference count, cleaning up the object if necessary | ||
* @param ref Counter to decremnt | ||
* @param cleanup Function to call to destroy | ||
* @returns True if object was removed, false otherwise | ||
*/ | ||
static inline bool tbx_obj_put(tbx_obj_t *obj) { | ||
return tbx_ref_put(&obj->refcount, obj->vtable->free_fn); | ||
} | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
TEST_DECLARE(always_win) | ||
|
||
TEST_DECLARE(tb_object) | ||
TEST_DECLARE(tb_object_api) | ||
TEST_DECLARE(tb_ref) | ||
TEST_DECLARE(tb_stack) | ||
TEST_DECLARE(tb_stk_escape_text) | ||
TEST_DECLARE(tb_ref) | ||
|
||
TASK_LIST_START | ||
TEST_ENTRY(always_win) | ||
TEST_ENTRY(tb_object) | ||
TEST_ENTRY(tb_object_api) | ||
TEST_ENTRY(tb_ref) | ||
TEST_ENTRY(tb_stack) | ||
TEST_ENTRY(tb_stk_escape_text) | ||
TEST_ENTRY(tb_ref) | ||
TASK_LIST_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "task.h" | ||
#include <tbx/object.h> | ||
#include <stdio.h> | ||
|
||
typedef struct { | ||
tbx_vtable_t base; | ||
void (*do_counter)(tbx_ref_t *ref); | ||
} test_vtable_t; | ||
|
||
typedef struct { | ||
tbx_obj_t desc; | ||
int counter; | ||
} test_obj_t; | ||
|
||
static int counter = 0; | ||
static void inc_counter(tbx_ref_t *ref) { | ||
counter++; | ||
} | ||
|
||
static void dec_counter(tbx_ref_t *ref) { | ||
counter--; | ||
} | ||
|
||
static void inc_obj_counter(tbx_ref_t *desc) { | ||
test_obj_t *obj = (test_obj_t *) container_of(desc, tbx_obj_t, refcount); | ||
(obj->counter)++; | ||
} | ||
|
||
static void dec_obj_counter(tbx_ref_t *desc) { | ||
test_obj_t *obj = (test_obj_t *) container_of(desc, tbx_obj_t, refcount); | ||
(obj->counter)--; | ||
} | ||
|
||
const test_vtable_t vtableA = { .base.name = "TestA", | ||
.base.free_fn = inc_obj_counter, | ||
.do_counter = inc_counter }; | ||
|
||
const test_vtable_t vtableB = { .base.name = "TestB", | ||
.base.free_fn = dec_obj_counter, | ||
.do_counter = dec_counter }; | ||
|
||
TEST_IMPL(tb_object) { | ||
test_obj_t objA; | ||
objA.counter = 0; | ||
objA.desc.vtable = &vtableA.base; | ||
tbx_ref_init(&objA.desc.refcount); | ||
|
||
test_obj_t objB; | ||
objB.counter = 0; | ||
objB.desc.vtable = &vtableB.base; | ||
tbx_ref_init(&objB.desc.refcount); | ||
|
||
// Test access to vtable base (type punning) | ||
ASSERT(strcmp("TestA", ((tbx_vtable_t *)objA.desc.vtable)->name) == 0); | ||
ASSERT(strcmp("TestB", ((tbx_vtable_t *)objB.desc.vtable)->name) == 0); | ||
|
||
// Test proper vtable access | ||
((test_vtable_t *) objB.desc.vtable)->do_counter(NULL); | ||
ASSERT(counter == -1); | ||
((test_vtable_t *) objA.desc.vtable)->do_counter(NULL); | ||
ASSERT(counter == 0); | ||
((test_vtable_t *) objB.desc.vtable)->do_counter(NULL); | ||
ASSERT(counter == -1); | ||
((test_vtable_t *) objB.desc.vtable)->do_counter(NULL); | ||
ASSERT(counter == -2); | ||
((test_vtable_t *) objB.desc.vtable)->do_counter(NULL); | ||
ASSERT(counter == -3); | ||
|
||
// Test refcounting and free pointer deallocation | ||
tbx_ref_put(&objA.desc.refcount, ((tbx_vtable_t *) objA.desc.vtable)->free_fn); | ||
tbx_ref_put(&objB.desc.refcount, ((tbx_vtable_t *) objB.desc.vtable)->free_fn); | ||
ASSERT(objA.counter == 1); | ||
ASSERT(objB.counter == -1); | ||
|
||
return 0; | ||
} | ||
|
||
TEST_IMPL(tb_object_api) { | ||
test_obj_t objA; | ||
objA.counter = 0; | ||
tbx_obj_init(&objA.desc, (tbx_vtable_t *) &vtableA); | ||
ASSERT(objA.counter == 0); | ||
|
||
tbx_obj_t *ret = tbx_obj_get(&objA.desc); | ||
ASSERT(objA.counter == 0); | ||
ASSERT(ret = &objA.desc); | ||
|
||
tbx_obj_put(&objA.desc); | ||
ASSERT(objA.counter == 0); | ||
|
||
tbx_obj_put(&objA.desc); | ||
ASSERT(objA.counter == 1); | ||
|
||
return 0; | ||
} |