aboutsummaryrefslogtreecommitdiff
path: root/src/keeper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/keeper.c')
-rw-r--r--src/keeper.c77
1 files changed, 38 insertions, 39 deletions
diff --git a/src/keeper.c b/src/keeper.c
index 8bece77..ea061ce 100644
--- a/src/keeper.c
+++ b/src/keeper.c
@@ -58,22 +58,22 @@
58 58
59typedef struct 59typedef struct
60{ 60{
61 int first; 61 lua_Integer first;
62 int count; 62 lua_Integer count;
63 int limit; 63 lua_Integer limit;
64} keeper_fifo; 64} keeper_fifo;
65 65
66// replaces the fifo ud by its uservalue on the stack 66// replaces the fifo ud by its uservalue on the stack
67static keeper_fifo* prepare_fifo_access( lua_State* L, int idx) 67static keeper_fifo* prepare_fifo_access( lua_State* L, int idx_)
68{ 68{
69 keeper_fifo* fifo = (keeper_fifo*) lua_touserdata( L, idx); 69 keeper_fifo* fifo = (keeper_fifo*) lua_touserdata( L, idx_);
70 if( fifo != NULL) 70 if( fifo != NULL)
71 { 71 {
72 idx = lua_absindex( L, idx); 72 idx_ = lua_absindex( L, idx_);
73 STACK_GROW( L, 1); 73 STACK_GROW( L, 1);
74 // we can replace the fifo userdata in the stack without fear of it being GCed, there are other references around 74 // we can replace the fifo userdata in the stack without fear of it being GCed, there are other references around
75 lua_getuservalue( L, idx); 75 lua_getuservalue( L, idx_);
76 lua_replace( L, idx); 76 lua_replace( L, idx_);
77 } 77 }
78 return fifo; 78 return fifo;
79} 79}
@@ -94,18 +94,18 @@ static void fifo_new( lua_State* L)
94 94
95// in: expect fifo ... on top of the stack 95// in: expect fifo ... on top of the stack
96// out: nothing, removes all pushed values from the stack 96// out: nothing, removes all pushed values from the stack
97static void fifo_push( lua_State* L, keeper_fifo* fifo, int _count) 97static void fifo_push( lua_State* L, keeper_fifo* fifo_, lua_Integer count_)
98{ 98{
99 int idx = lua_gettop( L) - _count; 99 int const idx = lua_gettop( L) - (int) count_;
100 int start = fifo->first + fifo->count - 1; 100 lua_Integer start = fifo_->first + fifo_->count - 1;
101 int i; 101 lua_Integer i;
102 // pop all additional arguments, storing them in the fifo 102 // pop all additional arguments, storing them in the fifo
103 for( i = _count; i >= 1; -- i) 103 for( i = count_; i >= 1; -- i)
104 { 104 {
105 // store in the fifo the value at the top of the stack at the specified index, popping it from the stack 105 // store in the fifo the value at the top of the stack at the specified index, popping it from the stack
106 lua_rawseti( L, idx, start + i); 106 lua_rawseti( L, idx, start + i);
107 } 107 }
108 fifo->count += _count; 108 fifo_->count += count_;
109} 109}
110 110
111// in: fifo 111// in: fifo
@@ -113,28 +113,28 @@ static void fifo_push( lua_State* L, keeper_fifo* fifo, int _count)
113// expects exactly 1 value on the stack! 113// expects exactly 1 value on the stack!
114// currently only called with a count of 1, but this may change in the future 114// currently only called with a count of 1, but this may change in the future
115// function assumes that there is enough data in the fifo to satisfy the request 115// function assumes that there is enough data in the fifo to satisfy the request
116static void fifo_peek( lua_State* L, keeper_fifo* fifo, int count_) 116static void fifo_peek( lua_State* L, keeper_fifo* fifo_, lua_Integer count_)
117{ 117{
118 int i; 118 int i;
119 STACK_GROW( L, count_); 119 STACK_GROW( L, count_);
120 for( i = 0; i < count_; ++ i) 120 for( i = 0; i < count_; ++ i)
121 { 121 {
122 lua_rawgeti( L, 1, fifo->first + i); 122 lua_rawgeti( L, 1, fifo_->first + i);
123 } 123 }
124} 124}
125 125
126// in: fifo 126// in: fifo
127// out: remove the fifo from the stack, push as many items as required on the stack (function assumes they exist in sufficient number) 127// out: remove the fifo from the stack, push as many items as required on the stack (function assumes they exist in sufficient number)
128static void fifo_pop( lua_State* L, keeper_fifo* fifo, int count_) 128static void fifo_pop( lua_State* L, keeper_fifo* fifo_, lua_Integer count_)
129{ 129{
130 int fifo_idx = lua_gettop( L); // ... fifo 130 int const fifo_idx = lua_gettop( L); // ... fifo
131 int i; 131 int i;
132 // each iteration pushes a value on the stack! 132 // each iteration pushes a value on the stack!
133 STACK_GROW( L, count_ + 2); 133 STACK_GROW( L, count_ + 2);
134 // skip first item, we will push it last 134 // skip first item, we will push it last
135 for( i = 1; i < count_; ++ i) 135 for( i = 1; i < count_; ++ i)
136 { 136 {
137 int const at = fifo->first + i; 137 lua_Integer const at = fifo_->first + i;
138 // push item on the stack 138 // push item on the stack
139 lua_rawgeti( L, fifo_idx, at); // ... fifo val 139 lua_rawgeti( L, fifo_idx, at); // ... fifo val
140 // remove item from the fifo 140 // remove item from the fifo
@@ -143,7 +143,7 @@ static void fifo_pop( lua_State* L, keeper_fifo* fifo, int count_)
143 } 143 }
144 // now process first item 144 // now process first item
145 { 145 {
146 int const at = fifo->first; 146 lua_Integer const at = fifo_->first;
147 lua_rawgeti( L, fifo_idx, at); // ... fifo vals val 147 lua_rawgeti( L, fifo_idx, at); // ... fifo vals val
148 lua_pushnil( L); // ... fifo vals val nil 148 lua_pushnil( L); // ... fifo vals val nil
149 lua_rawseti( L, fifo_idx, at); // ... fifo vals val 149 lua_rawseti( L, fifo_idx, at); // ... fifo vals val
@@ -151,23 +151,23 @@ static void fifo_pop( lua_State* L, keeper_fifo* fifo, int count_)
151 } 151 }
152 { 152 {
153 // avoid ever-growing indexes by resetting each time we detect the fifo is empty 153 // avoid ever-growing indexes by resetting each time we detect the fifo is empty
154 int const new_count = fifo->count - count_; 154 lua_Integer const new_count = fifo_->count - count_;
155 fifo->first = (new_count == 0) ? 1 : (fifo->first + count_); 155 fifo_->first = (new_count == 0) ? 1 : (fifo_->first + count_);
156 fifo->count = new_count; 156 fifo_->count = new_count;
157 } 157 }
158} 158}
159 159
160// in: linda_ud expected at *absolute* stack slot idx 160// in: linda_ud expected at *absolute* stack slot idx
161// out: fifos[ud] 161// out: fifos[ud]
162static void* const fifos_key = (void*) prepare_fifo_access; 162static void* const fifos_key = (void*) prepare_fifo_access;
163static void push_table( lua_State* L, int idx) 163static void push_table( lua_State* L, int idx_)
164{ 164{
165 STACK_GROW( L, 4); 165 STACK_GROW( L, 4);
166 STACK_CHECK( L); 166 STACK_CHECK( L);
167 idx = lua_absindex( L, idx); 167 idx_ = lua_absindex( L, idx_);
168 lua_pushlightuserdata( L, fifos_key); // ud fifos_key 168 lua_pushlightuserdata( L, fifos_key); // ud fifos_key
169 lua_rawget( L, LUA_REGISTRYINDEX); // ud fifos 169 lua_rawget( L, LUA_REGISTRYINDEX); // ud fifos
170 lua_pushvalue( L, idx); // ud fifos ud 170 lua_pushvalue( L, idx_); // ud fifos ud
171 lua_rawget( L, -2); // ud fifos fifos[ud] 171 lua_rawget( L, -2); // ud fifos fifos[ud]
172 STACK_MID( L, 2); 172 STACK_MID( L, 2);
173 if( lua_isnil( L, -1)) 173 if( lua_isnil( L, -1))
@@ -175,7 +175,7 @@ static void push_table( lua_State* L, int idx)
175 lua_pop( L, 1); // ud fifos 175 lua_pop( L, 1); // ud fifos
176 // add a new fifos table for this linda 176 // add a new fifos table for this linda
177 lua_newtable( L); // ud fifos fifos[ud] 177 lua_newtable( L); // ud fifos fifos[ud]
178 lua_pushvalue( L, idx); // ud fifos fifos[ud] ud 178 lua_pushvalue( L, idx_); // ud fifos fifos[ud] ud
179 lua_pushvalue( L, -2); // ud fifos fifos[ud] ud fifos[ud] 179 lua_pushvalue( L, -2); // ud fifos fifos[ud] ud fifos[ud]
180 lua_rawset( L, -4); // ud fifos fifos[ud] 180 lua_rawset( L, -4); // ud fifos fifos[ud]
181 } 181 }
@@ -183,16 +183,16 @@ static void push_table( lua_State* L, int idx)
183 STACK_END( L, 1); 183 STACK_END( L, 1);
184} 184}
185 185
186int keeper_push_linda_storage( struct s_Universe* U, lua_State* L, void* ptr, unsigned long magic_) 186int keeper_push_linda_storage( struct s_Universe* U, lua_State* L, void* ptr_, unsigned long magic_)
187{ 187{
188 struct s_Keeper* K = keeper_acquire( U->keepers, magic_); 188 struct s_Keeper* const K = keeper_acquire( U->keepers, magic_);
189 lua_State* KL = K ? K->L : NULL; 189 lua_State* const KL = K ? K->L : NULL;
190 if( KL == NULL) return 0; 190 if( KL == NULL) return 0;
191 STACK_GROW( KL, 4); 191 STACK_GROW( KL, 4);
192 STACK_CHECK( KL); 192 STACK_CHECK( KL);
193 lua_pushlightuserdata( KL, fifos_key); // fifos_key 193 lua_pushlightuserdata( KL, fifos_key); // fifos_key
194 lua_rawget( KL, LUA_REGISTRYINDEX); // fifos 194 lua_rawget( KL, LUA_REGISTRYINDEX); // fifos
195 lua_pushlightuserdata( KL, ptr); // fifos ud 195 lua_pushlightuserdata( KL, ptr_); // fifos ud
196 lua_rawget( KL, -2); // fifos storage 196 lua_rawget( KL, -2); // fifos storage
197 lua_remove( KL, -2); // storage 197 lua_remove( KL, -2); // storage
198 if( !lua_istable( KL, -1)) 198 if( !lua_istable( KL, -1))
@@ -323,11 +323,11 @@ int keepercall_receive( lua_State* L)
323//in: linda_ud key mincount [maxcount] 323//in: linda_ud key mincount [maxcount]
324int keepercall_receive_batched( lua_State* L) 324int keepercall_receive_batched( lua_State* L)
325{ 325{
326 int const min_count = (int) lua_tointeger( L, 3); 326 lua_Integer const min_count = lua_tointeger( L, 3);
327 if( min_count > 0) 327 if( min_count > 0)
328 { 328 {
329 keeper_fifo* fifo; 329 keeper_fifo* fifo;
330 int const max_count = (int) luaL_optinteger( L, 4, min_count); 330 lua_Integer const max_count = luaL_optinteger( L, 4, min_count);
331 lua_settop( L, 2); // ud key 331 lua_settop( L, 2); // ud key
332 lua_insert( L, 1); // key ud 332 lua_insert( L, 1); // key ud
333 push_table( L, 2); // key ud fifos 333 push_table( L, 2); // key ud fifos
@@ -357,7 +357,7 @@ int keepercall_receive_batched( lua_State* L)
357int keepercall_limit( lua_State* L) 357int keepercall_limit( lua_State* L)
358{ 358{
359 keeper_fifo* fifo; 359 keeper_fifo* fifo;
360 int limit = (int) lua_tointeger( L, 3); 360 lua_Integer limit = lua_tointeger( L, 3);
361 push_table( L, 1); // ud key n fifos 361 push_table( L, 1); // ud key n fifos
362 lua_replace( L, 1); // fifos key n 362 lua_replace( L, 1); // fifos key n
363 lua_pop( L, 1); // fifos key 363 lua_pop( L, 1); // fifos key
@@ -429,7 +429,7 @@ int keepercall_set( lua_State* L)
429 } 429 }
430 else // set/replace contents stored at the specified key? 430 else // set/replace contents stored at the specified key?
431 { 431 {
432 int count = lua_gettop( L) - 2; // number of items we want to store 432 lua_Integer count = lua_gettop( L) - 2; // number of items we want to store
433 keeper_fifo* fifo; // fifos key [val [, ...]] 433 keeper_fifo* fifo; // fifos key [val [, ...]]
434 lua_pushvalue( L, 2); // fifos key [val [, ...]] key 434 lua_pushvalue( L, 2); // fifos key [val [, ...]] key
435 lua_rawget( L, 1); // fifos key [val [, ...]] fifo|nil 435 lua_rawget( L, 1); // fifos key [val [, ...]] fifo|nil
@@ -466,7 +466,7 @@ int keepercall_set( lua_State* L)
466int keepercall_get( lua_State* L) 466int keepercall_get( lua_State* L)
467{ 467{
468 keeper_fifo* fifo; 468 keeper_fifo* fifo;
469 int count = 1; 469 lua_Integer count = 1;
470 if( lua_gettop( L) == 3) // ud key count 470 if( lua_gettop( L) == 3) // ud key count
471 { 471 {
472 count = lua_tointeger( L, 3); 472 count = lua_tointeger( L, 3);
@@ -482,7 +482,7 @@ int keepercall_get( lua_State* L)
482 count = __min( count, fifo->count); 482 count = __min( count, fifo->count);
483 // read <count> value off the fifo 483 // read <count> value off the fifo
484 fifo_peek( L, fifo, count); // fifo ... 484 fifo_peek( L, fifo, count); // fifo ...
485 return count; 485 return (int) count;
486 } 486 }
487 // no fifo was ever registered for this key, or it is empty 487 // no fifo was ever registered for this key, or it is empty
488 return 0; 488 return 0;
@@ -491,7 +491,6 @@ int keepercall_get( lua_State* L)
491// in: linda_ud [, key [, ...]] 491// in: linda_ud [, key [, ...]]
492int keepercall_count( lua_State* L) 492int keepercall_count( lua_State* L)
493{ 493{
494 int top;
495 push_table( L, 1); // ud keys fifos 494 push_table( L, 1); // ud keys fifos
496 switch( lua_gettop( L)) 495 switch( lua_gettop( L))
497 { 496 {
@@ -537,7 +536,7 @@ int keepercall_count( lua_State* L)
537 lua_replace( L, 1); // out keys fifos 536 lua_replace( L, 1); // out keys fifos
538 // shifts all keys up in the stack. potentially slow if there are a lot of them, but then it should be bearable 537 // shifts all keys up in the stack. potentially slow if there are a lot of them, but then it should be bearable
539 lua_insert( L, 2); // out fifos keys 538 lua_insert( L, 2); // out fifos keys
540 while( (top = lua_gettop( L)) > 2) 539 while( lua_gettop( L) > 2)
541 { 540 {
542 keeper_fifo* fifo; 541 keeper_fifo* fifo;
543 lua_pushvalue( L, -1); // out fifos keys key 542 lua_pushvalue( L, -1); // out fifos keys key