Beispiel #1
0
static int _get_new_node(struct word_filter_t *flt)
{
    if (!flt)
    {
        return -1;
    }

    bsp_spin_lock(&flt->node_lock);
    int ret = flt->node_used, i;
    int block_id = ret / BLOCK_NODE_SIZE;
    struct hash_tree_node_t **tmp;
    while (block_id >= flt->node_list_size)
    {
        int nlen = (flt->node_list_size) ? flt->node_list_size * 2 : BLOCK_LIST_INITIAL;
        tmp = bsp_realloc(flt->node_list, nlen * sizeof(struct hash_tree_node_t *));
        if (tmp)
        {
            for (i = flt->node_list_size; i < nlen; i ++)
            {
                tmp[i] = NULL;
            }
            
            flt->node_list = tmp;
            flt->node_list_size = nlen;
        }

        else
        {
            bsp_spin_unlock(&flt->node_lock);
            return -1;
        }
    }

    // If block
    if (!flt->node_list[block_id])
    {
        struct hash_tree_node_t *block = bsp_calloc(BLOCK_NODE_SIZE, sizeof(struct hash_tree_node_t));
        if (block)
        {
            flt->node_list[block_id] = block;
        }

        else
        {
            bsp_spin_unlock(&flt->node_lock);
            return -1;
        }
    }

    flt->node_used ++;
    bsp_spin_unlock(&flt->node_lock);
    
    return ret;
}
Beispiel #2
0
// Output some message
void debug_printf(const char *fmt, ...)
{
    time_t now = time((time_t *) NULL);
    struct tm *loctime;
    char tgdate[64];
    char msg[MAX_TRACE_LENGTH];

    va_list ap;
    va_start(ap, fmt);
    size_t nbytes = vsnprintf(msg, MAX_TRACE_LENGTH - 1, fmt, ap);
    if (nbytes >= 0)
    {
        msg[nbytes] = 0;
    }
    va_end(ap);

    loctime = localtime(&now);
    strftime(tgdate, 64, "%m/%d/%Y %H:%M:%S", loctime);
    bsp_spin_lock(&debug_lock);
    fprintf(stderr, "\n\033[1;37m=== [Debug String] === <%s START> ===\033[0m\n"
                    "\033[1;33m %s\033[0m\n"
                    "\033[1;37m=== [Debug String] === <%s END  > ===\033[0m\n\n", tgdate, msg, tgdate);
    bsp_spin_unlock(&debug_lock);

    return;
}
Beispiel #3
0
// Print some formatted message with terminal color
void trace_output(time_t now, int level, const char *msg)
{
    struct tm *loctime;
    char tgdate[64];

    if (!level || !msg)
    {
        return;
    }

    loctime = localtime(&now);
    strftime(tgdate, 64, "%m/%d/%Y %H:%M:%S", loctime);
    bsp_spin_lock(&debug_lock);
    fprintf(stderr, "\033[1;37m[\033[0m"
                    "\033[0;36m%s\033[0m"
                    "\033[1;37m]\033[0m"
                    " - "
                    "\033[1;37m[\033[0m"
                    "%s"
                    "\033[1;37m]\033[0m"
                    " : %s\n", tgdate, get_trace_level_str(level, 1), msg);
    bsp_spin_unlock(&debug_lock);

    return;
}
Beispiel #4
0
void debug_value(BSP_VALUE *val)
{
    if (!val)
    {
        bsp_spin_lock(&debug_lock);
        fprintf(stderr, "\n\033[1;37m === [NOTHING TO DEBUG] ===\033[0m\n\n");
        bsp_spin_unlock(&debug_lock);

        return;
    }

    bsp_spin_lock(&debug_lock);
    fprintf(stderr, "\n\033[1;37m=== [Debug Value] === < START > ===\033[0m\n");
    _dump_value(val, 0);
    fprintf(stderr, "\033[1;37m=== [Debug Value] === < END > ===\033[0m\n\n");
    bsp_spin_unlock(&debug_lock);

    return;
}
Beispiel #5
0
void debug_object(BSP_OBJECT *obj)
{
    if (!obj)
    {
        bsp_spin_lock(&debug_lock);
        fprintf(stderr, "\n\033[1;37m === [NOTHING TO DEBUG] ===\033[0m\n\n");
        bsp_spin_unlock(&debug_lock);

        return;
    }

    bsp_spin_lock(&debug_lock);
    fprintf(stderr, "\n\033[1;37m=== [Debug Object] === < START > ===\033[0m\n");
    _dump_object(obj, 0);
    fprintf(stderr, "\033[1;37m=== [Debug Object] === < END > ===\033[0m\n\n");
    bsp_spin_unlock(&debug_lock);

    return;
}
Beispiel #6
0
// Print lua stack
void debug_lua_stack(lua_State *l)
{
    if (!l)
    {
        return;
    }

    bsp_spin_lock(&debug_lock);
    fprintf(stderr, "\n\033[1;33m== [Stack top] ==\033[0m\n");
    int size = lua_gettop(l), n, type;
    for (n = 1; n <= size; n ++)
    {
        type = lua_type(l, n);
        switch (type)
        {
            case LUA_TNIL : 
                fprintf(stderr, "\t\033[1;37m[NIL]\033[0m\n");
                break;
            case LUA_TNUMBER : 
                fprintf(stderr, "\t\033[1;37m[NUMBER]    => %f\033[0m\n", lua_tonumber(l, n));
                break;
            case LUA_TBOOLEAN : 
                fprintf(stderr, "\t\033[1;37m[BOOLEAN]   => %d\033[0m\n", lua_toboolean(l, n));
                break;
            case LUA_TSTRING : 
                fprintf(stderr, "\t\033[1;37m[STRING]    => %s\033[0m\n", lua_tostring(l, n));
                break;
            case LUA_TTABLE : 
                fprintf(stderr, "\t\033[1;37m[TABLE]\033[0m\n");
                break;
            case LUA_TFUNCTION : 
                fprintf(stderr, "\t\033[1;37m[FUNCTION]\033[0m\n");
                break;
            case LUA_TUSERDATA : 
                fprintf(stderr, "\t\033[1;37m[USERDATA]  => %p\033[0m\n", lua_touserdata(l, n));
                break;
            case LUA_TTHREAD : 
                fprintf(stderr, "\t\033[1;37m[THREAD]\033[0m\n");
                break;
            case LUA_TLIGHTUSERDATA : 
                fprintf(stderr, "\t\033[1;37m[LUSERDATA] => %p\033[0m\n", lua_touserdata(l, n));
                break;
            default : 
                break;
        }
    }
    fprintf(stderr, "\033[1;33m== [Stack button] ==\033[0m\n\n");
    bsp_spin_unlock(&debug_lock);

    return;
}
Beispiel #7
0
// Print one memory block as also human-readable and a hexadecimal table
void debug_hex(const char *data, ssize_t len)
{
    if (!data)
    {
        fprintf(stderr, "\n\033[1;36m=== [NOTHING TO OUTPUT] ===\033[0m\n");
        return;
    }

    int i;
    if (len < 0)
    {
        len = strlen(data);
    }

    time_t now = time((time_t *) NULL);
    struct tm *loctime;
    char tgdate[64];
    loctime = localtime(&now);
    strftime(tgdate, 64, "%m/%d/%Y %H:%M:%S", loctime);
    bsp_spin_lock(&debug_lock);
    fprintf(stderr, "\n\033[1;37m=== [Debug Hex %d bytes] === <%s ORIGIN > ===\033[0m\n", (int) len, tgdate);
    for (i = 0; i < len; i ++)
    {
        fprintf(stderr, "\033[1;33m%02X\033[0m ", (unsigned char) data[i]);
        if (i % 32 == 31)
        {
            fprintf(stderr, "\n");
        }
        else if (i % 8 == 7)
        {
            fprintf(stderr, "  ");
        }
    }

    fprintf(stderr, "\n\033[1;37m=== [Debug Hex %d bytes] === <%s DATA   > ===\033[0m\n", (int) len, tgdate);
    for (i = 0; i < len; i ++)
    {
        if (data[i] >= 32 && data[i] <= 127)
        {
            fprintf(stderr, "\033[1;35m %c \033[0m", data[i]);
        }
        else
        {
            fprintf(stderr, "\033[0;34m . \033[0m");
        }

        if (i % 32 == 31)
        {
            fprintf(stderr, "\n");
        }
        else if (i % 8 == 7)
        {
            fprintf(stderr, "  ");
        }
    }

    fprintf(stderr, "\n\033[1;37m=== [Debug Hex %d bytes] === <%s END    > ===\033[0m\n\n", (int) len, tgdate);
    bsp_spin_unlock(&debug_lock);

    return;
}