Navigation Menu

Skip to content

Commit

Permalink
Keep 8 handles unused
Browse files Browse the repository at this point in the history
This guarantees at least some handle number rotation
even in the worst case.

Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
  • Loading branch information
sustrik committed Apr 12, 2018
1 parent 0b3fb92 commit 3f00ae9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 7 additions & 2 deletions handle.c
Expand Up @@ -55,6 +55,7 @@ struct dill_handle {
int dill_ctx_handle_init(struct dill_ctx_handle *ctx) {
ctx->handles = NULL;
ctx->nhandles = 0;
ctx->nused = 0;
ctx->first = -1;
ctx->last = -1;
return 0;
Expand All @@ -71,8 +72,10 @@ int hmake(struct hvfs *vfs) {
/* Returns ECANCELED if shutting down. */
int rc = dill_canblock();
if(dill_slow(rc < 0)) return -1;
/* If there's no space for the new handle, expand the array. */
if(dill_slow(ctx->first == -1)) {
/* If there's no space for the new handle, expand the array.
Keep at least 8 handles unused so that there's at least some rotation
of handle numbers even if operating close to the current limit. */
if(dill_slow(ctx->nhandles - ctx->nused <= 8)) {
/* Start with 256 handles, double the size when needed. */
int sz = ctx->nhandles ? ctx->nhandles * 2 : 256;
struct dill_handle *hndls =
Expand All @@ -98,6 +101,7 @@ int hmake(struct hvfs *vfs) {
ctx->handles[h].next = -2;
ctx->handles[h].type = NULL;
ctx->handles[h].ptr = NULL;
ctx->nused++;
return h;
}

Expand Down Expand Up @@ -151,6 +155,7 @@ int hclose(int h) {
if(ctx->first == -1) ctx->first = h;
else ctx->handles[ctx->last].next = h;
ctx->last = h;
ctx->nused--;
return 0;
}

Expand Down
4 changes: 3 additions & 1 deletion handle.h
Expand Up @@ -26,9 +26,11 @@
struct dill_handle;

struct dill_ctx_handle {
/* Array of handles. The size of the array is stored in 'nhandles'. */
/* Array of handles. The size of the array is stored in 'nall'. */
struct dill_handle *handles;
int nhandles;
/* Number of allocated handles. */
int nused;
/* Points to the first and last item in the list of unused handles. */
int first;
int last;
Expand Down

0 comments on commit 3f00ae9

Please sign in to comment.