Write the reffunction API

This commit is contained in:
2023-06-07 03:07:47 +10:00
committed by GitHub
parent bbbdf8a3b7
commit a8059fa3fd
2 changed files with 47 additions and 1 deletions

View File

@@ -0,0 +1,46 @@
# toy_reffunction.h
This header defines the structure `Toy_RefFunction`, as well as all of the related utilities.
See [toy_RefString](toy_refstring_h.md) for more information about the reference pattern.
This module reserves the right to instead preform a deep copy when it sees fit (this is for future debugging purposes).
## Defined Interfaces
### typedef void* (*Toy_RefFunctionAllocatorFn)(void* pointer, size_t oldSize, size_t newSize)
This interface conforms to Toy's memory API, and generally shouldn't be used.
## Defined Functions
### void Toy_setRefFunctionAllocatorFn(Toy_RefFunctionAllocatorFn)
This function conforms to and is invoked by Toy's memory API, and generally shouldn't be used.
### Toy_RefFunction* Toy_createRefFunction(const void* data, size_t length)
This function returns a new `Toy_RefFunction`, containing a copy of `data`, or `NULL` on error.
This function also sets the returned `refFunction`'s reference counter to 1.
### void Toy_deleteRefFunction(Toy_RefFunction* refFunction)
This function reduces the `refFunction`'s reference counter by 1 and, if it reaches 0, frees the memory.
### int Toy_countRefFunction(Toy_RefFunction* refFunction)
This function returns the total number of references to `refFunction`, for debugging.
### size_t Toy_lengthRefFunction(Toy_RefFunction* refFunction)
This function returns the length of the underlying bytecode of `refFunction`.
### Toy_RefFunction* Toy_copyRefFunction(Toy_RefFunction* refFunction)
This function increases the reference counter of `refFunction` by 1, before returning the given pointer.
### Toy_RefFunction* Toy_deepCopyRefFunction(Toy_RefFunction* refFunction)
This function behaves identically to `Toy_copyRefFunction`, except that it explicitly preforms a deep copy of the internal memory. Using this function should be done carefully, as it incurs a performance penalty that negates the benefit of this module.

View File

@@ -81,6 +81,6 @@ print tally(); //3
* [toy_literal.h](c-api/toy_literal_h.md)
* [toy_memory.h](c-api/toy_memory_h.md)
* [toy_parser.h](c-api/toy_parser_h.md)
* toy_reffunction.h (docs coming soon)
* [toy_reffunction.h](c-api/toy_reffunction_h.md)
* [toy_refstring.h](c-api/toy_refstring_h.md)
* [toy_scope.h](c-api/toy_scope_h.md)