Skip to content

Commit

Permalink
Document which errors the C functions can return. (#2315)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed May 16, 2024
1 parent f5534eb commit 55101f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
30 changes: 22 additions & 8 deletions include/toit/toit.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ typedef struct toit_msg_cbs_t {
* @param id The unique identifier for the message handler.
* @param user_data The user data to pass to the callbacks.
* @param cbs The callbacks for the message handler.
* @return toit_err_t The result of adding the message handler.
* @return toit_err_t
* - TOIT_OK: The message handler was added successfully.
* - TOIT_ERR_OOM: Out of memory.
*/
toit_err_t toit_msg_add_handler(const char* id,
void* user_data,
Expand All @@ -165,7 +167,9 @@ toit_err_t toit_msg_add_handler(const char* id,
* Once the message handler is removed, the `on_removed` callback will be called.
*
* @param context The context of the message handler.
* @return toit_err_t The result of the call.
* @return toit_err_t
* - TOIT_OK: The message handler was removed successfully.
* - TOIT_ERR_NOT_FOUND: The message handler was not found.
*/
toit_err_t toit_msg_remove_handler(toit_msg_context_t* context);

Expand All @@ -187,7 +191,10 @@ toit_err_t toit_msg_remove_handler(toit_msg_context_t* context);
* @param data The data to send.
* @param length The length of the data.
* @param free_on_failure Whether to free the data if the message cannot be sent.
* @return toit_err_t The result of the call.
* @return toit_err_t
* - TOIT_OK: The message was sent successfully.
* - TOIT_ERR_NO_SUCH_RECEIVER: The target process does not exist.
* - TOIT_ERR_OOM: Out of memory, despite calling `toit_gc`.
*/
toit_err_t toit_msg_notify(toit_msg_context_t* context,
int target_pid,
Expand All @@ -208,7 +215,10 @@ toit_err_t toit_msg_notify(toit_msg_context_t* context,
* @param data The data to send.
* @param length The length of the data.
* @param free_on_failure Whether to free the data if the message cannot be sent.
* @return toit_err_t The result of the call.
* @return toit_err_t
* - TOIT_OK: The message was sent successfully.
* - TOIT_ERR_NO_SUCH_RECEIVER: The target process does not exist.
* - TOIT_ERR_OOM: Out of memory, despite calling `toit_gc`.
*/
toit_err_t toit_msg_request_reply(toit_msg_request_handle_t handle, uint8_t* data, int length, bool free_on_failure);

Expand All @@ -224,14 +234,18 @@ toit_err_t toit_msg_request_reply(toit_msg_request_handle_t handle, uint8_t* dat
* @param handle The handle to the request. This handle must have been received
* through the `on_rpc_request` callback.
* @param error The error message to send.
* @return toit_err_t The result of the call.
* @return toit_err_t
* - TOIT_OK: The failure message was sent successfully.
* - TOIT_ERR_NO_SUCH_RECEIVER: The target process does not exist.
* - TOIT_ERR_OOM: Out of memory, despite calling `toit_gc`.
*/
toit_err_t toit_msg_request_fail(toit_msg_request_handle_t handle, const char* error);

/**
* @brief Perform a garbage collection on all Toit processes.
*
* @return toit_err_t The result of the call.
* @return toit_err_t
* - TOIT_OK: The garbage collection was run successfully.
*/
toit_err_t toit_gc();

Expand Down Expand Up @@ -262,8 +276,8 @@ void* toit_calloc(size_t nmemb, size_t size);
* If `realloc` fails, this function calls `toit_gc` and then retries the allocation.
*
* @param ptr The pointer to the memory to reallocate.
* @param size The size of the memory to allocate.
* @return void* A pointer to the allocated memory, or `NULL` if the allocation failed.
* @param size The new size that is requested.
* @return void* A pointer to the allocated memory, or `NULL` if the reallocation failed.
*/
void* toit_realloc(void* ptr, size_t size);

Expand Down
2 changes: 1 addition & 1 deletion src/messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ toit_err_t toit_msg_add_handler(const char* id,
auto old = toit::registered_message_handlers;
auto list = toit::unvoid_cast<toit::RegisteredExternalMessageHandlerList*>(
malloc(sizeof(toit::RegisteredExternalMessageHandlerList)));
if (list == null) FATAL("[OOM during external process setup]");
if (list == null) return TOIT_ERR_OOM;
list->next = old;
list->registered_handler.id = id;
list->registered_handler.user_context = user_context;
Expand Down

0 comments on commit 55101f8

Please sign in to comment.