@@ -233,6 +233,20 @@ typedef struct hyper_waker hyper_waker;
233233
234234typedef int (*hyper_body_foreach_callback)(void *, const struct hyper_buf *);
235235
236+ /*
237+ Many hyper entites can be given userdata to allow user callbacks to corellate work together.
238+ Since much of hyper is asychronous it's often useful to treat these userdata objects as "owned"
239+ by the hyper entity (and hence to be cleaned up when that entity is dropped).
240+
241+ To acheive this a `hyepr_userdata_drop` callback is passed by calling code alongside the
242+ userdata to register a cleanup function.
243+
244+ This function may be provided as NULL if the calling code wants to manage memory lifetimes
245+ itself, in which case the hyper object will logically consider the userdata "borrowed" until
246+ the hyper entity is dropped.
247+ */
248+ typedef void (*hyper_userdata_drop)(void *);
249+
236250typedef int (*hyper_body_data_callback)(void *, struct hyper_context *, struct hyper_buf **);
237251
238252/*
@@ -252,20 +266,10 @@ typedef int (*hyper_body_data_callback)(void*, struct hyper_context*, struct hyp
252266 */
253267typedef void (*hyper_service_callback)(void *, struct hyper_request *, struct hyper_response_channel *);
254268
255- /*
256- Since a hyper_service owns the userdata passed to it the calling code can register a cleanup
257- function to be called when the service is dropped. This function may be omitted/a no-op if
258- the calling code wants to manage memory lifetimes itself (e.g. if the userdata is a static
259- global)
260- */
261- typedef void (*hyper_service_userdata_drop_callback)(void *);
262-
263269typedef void (*hyper_request_on_informational_callback)(void *, struct hyper_response *);
264270
265271typedef int (*hyper_headers_foreach_callback)(void *, const uint8_t *, size_t , const uint8_t *, size_t );
266272
267- typedef void (*hyper_io_userdata_drop_callback)(void *);
268-
269273typedef size_t (*hyper_io_read_callback)(void *, struct hyper_context *, uint8_t *, size_t );
270274
271275typedef size_t (*hyper_io_write_callback)(void *, struct hyper_context *, const uint8_t *, size_t );
@@ -319,12 +323,13 @@ struct hyper_task *hyper_body_data(struct hyper_body *body);
319323 */
320324struct hyper_task *hyper_body_foreach (struct hyper_body *body,
321325 hyper_body_foreach_callback func,
322- void *userdata);
326+ void *userdata,
327+ hyper_userdata_drop drop);
323328
324329/*
325330 Set userdata on this body, which will be passed to callback functions.
326331 */
327- void hyper_body_set_userdata (struct hyper_body *body, void *userdata);
332+ void hyper_body_set_userdata (struct hyper_body *body, void *userdata, hyper_userdata_drop drop );
328333
329334/*
330335 Set the data callback for this body.
@@ -659,12 +664,13 @@ struct hyper_service *hyper_service_new(hyper_service_callback service_fn);
659664
660665 The service takes ownership of the userdata and will call the `drop_userdata` callback when
661666 the service task is complete. If the `drop_userdata` callback is `NULL` then the service
662- will instead forget the userdata when the associated task is completed and the calling code
663- is responsible for cleaning up the userdata through some other mechanism.
667+ will instead borrow the userdata and forget it when the associated task is completed and
668+ thus the calling code is responsible for cleaning up the userdata through some other
669+ mechanism.
664670 */
665671void hyper_service_set_userdata (struct hyper_service *service,
666672 void *userdata,
667- hyper_service_userdata_drop_callback drop_func );
673+ hyper_userdata_drop drop );
668674
669675/*
670676 Frees a hyper_service object if no longer needed
@@ -896,7 +902,8 @@ struct hyper_body *hyper_request_body(struct hyper_request *req);
896902 */
897903enum hyper_code hyper_request_on_informational (struct hyper_request *req,
898904 hyper_request_on_informational_callback callback,
899- void *data);
905+ void *data,
906+ hyper_userdata_drop drop);
900907
901908/*
902909 Construct a new HTTP 200 Ok response
@@ -1047,9 +1054,7 @@ void hyper_io_free(struct hyper_io *io);
10471054 `hyper_io` is destroyed (either explicitely by `hyper_io_free` or
10481055 implicitely by an associated hyper task completing).
10491056 */
1050- void hyper_io_set_userdata (struct hyper_io *io,
1051- void *data,
1052- hyper_io_userdata_drop_callback drop_func);
1057+ void hyper_io_set_userdata (struct hyper_io *io, void *data, hyper_userdata_drop drop_func);
10531058
10541059/*
10551060 Get the user data pointer for this IO value.
@@ -1161,7 +1166,7 @@ enum hyper_task_return_type hyper_task_type(struct hyper_task *task);
11611166 This value will be passed to task callbacks, and can be checked later
11621167 with `hyper_task_userdata`.
11631168 */
1164- void hyper_task_set_userdata (struct hyper_task *task, void *userdata);
1169+ void hyper_task_set_userdata (struct hyper_task *task, void *userdata, hyper_userdata_drop drop );
11651170
11661171/*
11671172 Retrieve the userdata that has been set via `hyper_task_set_userdata`.
0 commit comments