A lightweight Automatic Reference Counting (ARC) memory management system for C, inspired by Objective-C/Swift ARC patterns. Trove provides reference counting and autorelease pool functionality for C objects, simplifying memory management.
- Automatic Reference Counting: Track object lifetimes with retain/release semantics
- Autorelease Pools: Defer object deallocation for convenient memory management
- Scoped Memory Management: TROVE macro creates scoped autorelease blocks
- Type-Safe API: Consistent interface for creating and managing object lifecycles
- Minimal Dependencies: Standard C library only with no external dependencies
Clone the repository and build using make:
git clone https://github.com/wess/trove.git
cd trove
makeThe project will build a sample program in the build directory.
#include "macros.h"
#include <stdio.h>
int main() {
TROVE {
// Create an autoreleased string
TroveString *greeting = String("Hello, Trove ARC!");
printf("%s\n", greeting->str);
// No need to free memory - automatically handled when TROVE block ends
}
return 0;
}// Create an object with reference count 1
TroveString *str = TroveString_create("Manually managed");
// Increase reference count
RETAIN(str);
// Decrease reference count
RELEASE(str);
// Release again (reference count now 0, object is freed)
RELEASE(str);// Push a new autorelease pool
AUTORELEASE_POOL_PUSH();
// Create autoreleased objects
TroveString *str1 = String("Object 1");
TroveString *str2 = String("Object 2");
// Use objects...
// Pop the autorelease pool, releasing all autoreleased objects
AUTORELEASE_POOL_POP();ARCObject: Base structure for all ARC-managed objectsTroveString: Example implementation of an ARC-managed string type
arc_retain(): Increment an object's reference countarc_release(): Decrement an object's reference count and free if zeroarc_autorelease(): Add an object to the current autorelease pool
RETAIN(obj): Retain an objectRELEASE(obj): Release an objectString(text): Create an autoreleased stringTROVE { ... }: Create a scoped autorelease pool blockAUTORELEASE_POOL_PUSH(): Push a new autorelease poolAUTORELEASE_POOL_POP(): Pop the current autorelease pool
To create your own ARC-managed objects:
- Include
ARCObjectas the first member of your struct - Implement a create function that sets up the object with a reference count of 1
- Implement a dealloc function that frees resources when reference count reaches zero
- Optionally create convenience macros using
arc_autorelease()
This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.