Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
fixed & tested Shiny + ShinyLua
Browse files Browse the repository at this point in the history
  • Loading branch information
aidinabedi committed Dec 26, 2009
1 parent e2f1766 commit bd60055
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 106 deletions.
6 changes: 3 additions & 3 deletions Shiny/include/ShinyManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ SHINY_INLINE void _ShinyManager_uninit(ShinyManager *self) {
#if SHINY_LOOKUP_RATE == TRUE
SHINY_INLINE void _ShinyManager_incLookup(ShinyManager *self) { self->_lookupCount++; }
SHINY_INLINE void _ShinyManager_incLookupSuccess(ShinyManager *self) { self->_lookupSuccessCount++; }
SHINY_INLINE float ShinyManager_getLookupRate(const ShinyManager *self) { return ((float) self->_lookupSuccessCount) / ((float) self->_lookupCount); }
SHINY_INLINE float ShinyManager_lookupRate(const ShinyManager *self) { return ((float) self->_lookupSuccessCount) / ((float) self->_lookupCount); }

#else
SHINY_INLINE void _ShinyManager_incLookup(ShinyManager *self) {}
SHINY_INLINE void _ShinyManager_incLookupSuccess(ShinyManager *self) {}
SHINY_INLINE float ShinyManager_getLookupRate(const ShinyManager *self) { return -1; }
SHINY_INLINE float ShinyManager_lookupRate(const ShinyManager *self) { return -1; }
#endif

SHINY_API void ShinyManager_resetZones(ShinyManager *self);
Expand Down Expand Up @@ -182,7 +182,7 @@ SHINY_API void ShinyManager_destroy(ShinyManager *self);

SHINY_INLINE void ShinyManager_sortZones(ShinyManager *self) {
if (self->rootZone.next)
self->rootZone.next = ShinyZone_sortChain(self->rootZone.next);
self->_lastZone = ShinyZone_sortChain(&self->rootZone.next);
}

SHINY_API const char* ShinyManager_getOutputErrorString(ShinyManager *self);
Expand Down
2 changes: 1 addition & 1 deletion Shiny/include/ShinyZone.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ SHINY_API void ShinyZone_updateChainClean(ShinyZone *first);

SHINY_API void ShinyZone_resetChain(ShinyZone *first);

SHINY_API ShinyZone* ShinyZone_sortChain(ShinyZone *first);
SHINY_API ShinyZone* ShinyZone_sortChain(ShinyZone **first);

SHINY_INLINE float ShinyZone_compare(ShinyZone *a, ShinyZone *b) {
return a->data.selfTicks.avg - b->data.selfTicks.avg;
Expand Down
180 changes: 94 additions & 86 deletions Shiny/src/ShinyZone.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ THE SOFTWARE.

//-----------------------------------------------------------------------------

//TODO: these loops loop for ever - find out why.

void ShinyZone_preUpdateChain(ShinyZone *first) {
ShinyZone* zone = first;

Expand Down Expand Up @@ -85,93 +83,103 @@ void ShinyZone_resetChain(ShinyZone *first) {
by Philip J. Erdelsky
[email protected]
http:https://www.alumni.caltech.edu/~pje/
Modified by Aidin Abedi
*/

ShinyZone* ShinyZone_sortChain(ShinyZone *p)
ShinyZone* ShinyZone_sortChain(ShinyZone **first) // returns ptr to last zone
{
unsigned base;
unsigned long block_size;

struct tape
{
ShinyZone *first, *last;
unsigned long count;
} tape[4];

/* Distribute the records alternately to tape[0] and tape[1]. */

tape[0].count = tape[1].count = 0L;
tape[0].first = NULL;
base = 0;
while (p != NULL)
{
ShinyZone *next = p->next;
p->next = tape[base].first;
tape[base].first = p;
tape[base].count++;
p = next;
base ^= 1;
}

/* If the list is empty or contains only a single record, then */
/* tape[1].count == 0L and this part is vacuous. */

for (base = 0, block_size = 1L; tape[base+1].count != 0L;
base ^= 2, block_size <<= 1)
{
int dest;
struct tape *tape0, *tape1;
tape0 = tape + base;
tape1 = tape + base + 1;
dest = base ^ 2;
tape[dest].count = tape[dest+1].count = 0;
for (; tape0->count != 0; dest ^= 1)
{
unsigned long n0, n1;
struct tape *output_tape = tape + dest;
n0 = n1 = block_size;
while (1)
{
ShinyZone *chosen_record;
struct tape *chosen_tape;
if (n0 == 0 || tape0->count == 0)
{
if (n1 == 0 || tape1->count == 0)
break;
chosen_tape = tape1;
n1--;
}
else if (n1 == 0 || tape1->count == 0)
{
chosen_tape = tape0;
n0--;
}
else if (ShinyZone_compare(tape0->first, tape1->first) > 0)
{
chosen_tape = tape1;
n1--;
}
else
{
chosen_tape = tape0;
n0--;
}
chosen_tape->count--;
chosen_record = chosen_tape->first;
chosen_tape->first = chosen_record->next;
if (output_tape->count == 0)
output_tape->first = chosen_record;
else
output_tape->last->next = chosen_record;
output_tape->last = chosen_record;
output_tape->count++;
}
}
}

if (tape[base].count > 1L)
tape[base].last->next = NULL;
return tape[base].first;
ShinyZone *p = *first;

unsigned base;
unsigned long block_size;

struct tape
{
ShinyZone *first, *last;
unsigned long count;
} tape[4];

/* Distribute the records alternately to tape[0] and tape[1]. */

tape[0].count = tape[1].count = 0L;
tape[0].first = NULL;
base = 0;
while (p != NULL)
{
ShinyZone *next = p->next;
p->next = tape[base].first;
tape[base].first = p;
tape[base].count++;
p = next;
base ^= 1;
}

/* If the list is empty or contains only a single record, then */
/* tape[1].count == 0L and this part is vacuous. */

for (base = 0, block_size = 1L; tape[base+1].count != 0L;
base ^= 2, block_size <<= 1)
{
int dest;
struct tape *tape0, *tape1;
tape0 = tape + base;
tape1 = tape + base + 1;
dest = base ^ 2;
tape[dest].count = tape[dest+1].count = 0;
for (; tape0->count != 0; dest ^= 1)
{
unsigned long n0, n1;
struct tape *output_tape = tape + dest;
n0 = n1 = block_size;
while (1)
{
ShinyZone *chosen_record;
struct tape *chosen_tape;
if (n0 == 0 || tape0->count == 0)
{
if (n1 == 0 || tape1->count == 0)
break;
chosen_tape = tape1;
n1--;
}
else if (n1 == 0 || tape1->count == 0)
{
chosen_tape = tape0;
n0--;
}
else if (ShinyZone_compare(tape1->first, tape0->first) > 0)
{
chosen_tape = tape1;
n1--;
}
else
{
chosen_tape = tape0;
n0--;
}
chosen_tape->count--;
chosen_record = chosen_tape->first;
chosen_tape->first = chosen_record->next;
if (output_tape->count == 0)
output_tape->first = chosen_record;
else
output_tape->last->next = chosen_record;
output_tape->last = chosen_record;
output_tape->count++;
}
}
}

if (tape[base].count > 1L) {
ShinyZone* last = tape[base].last;
*first = tape[base].first;
last->next = NULL;
return last;

} else {
return NULL;
}
}


Expand Down
36 changes: 20 additions & 16 deletions ShinyLua/src/ShinyLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,30 @@ std::string StringPrint(const char* format, ...) {
//-----------------------------------------------------------------------------

Profile* FindProfile(lua_State *L, lua_Debug *ar) {
const void *key;
const void *func = NULL;

lua_getinfo(L, "f", ar);
key = lua_topointer(L, -1);
func = lua_topointer(L, -1);
lua_pop(L, 1);

Profile *prof = &profiles[key];
Profile *prof = &profiles[func];

if (!prof->zone.name) {
lua_getinfo(L, "nS", ar);
if (!ar->name) return NULL; // ignore tail calls
lua_getinfo(L, "S", ar);

switch (ar->what[0]) {
case 'C': /* 'C' */
prof->name = ar->name;
case 'L': // "Lua"
prof->name =
StringPrint("%s(%d):%s", ar->source, ar->linedefined, ar->name);
break;

case 'm': /* 'main' */
prof->name = ar->source;
case 'C': // "C"
prof->name = "C:";
prof->name += ar->name;
break;

default: /* 'Lua' */
prof->name = StringPrint("%s(%d):%s",
ar->source, ar->linedefined, ar->name);

default: // impossible happened
prof->name = "<unknown>";
}

prof->zone.name = prof->name.c_str();
Expand All @@ -143,6 +143,13 @@ Profile* FindProfile(lua_State *L, lua_Debug *ar) {
//-----------------------------------------------------------------------------

void callhook(lua_State *L, lua_Debug *ar) {
// ignore tail call
if (ar->i_ci == 0 || ar->event == LUA_HOOKTAILRET) return;

// ignore nameless function
lua_getinfo(L, "n", ar);
if (!ar->name) return;

switch (ar->event) {
case LUA_HOOKCALL:
{
Expand All @@ -154,9 +161,6 @@ void callhook(lua_State *L, lua_Debug *ar) {
case LUA_HOOKRET:
ShinyManager_endCurNode(&Shiny_instance);
return;

default:
return;
}
}

Expand Down

0 comments on commit bd60055

Please sign in to comment.