Giter Club home page Giter Club logo

Comments (5)

NathanFrench avatar NathanFrench commented on August 17, 2024

haha I can't believe I just did a pull request of the wrong code.

from libevhtp.

Turtleberry avatar Turtleberry commented on August 17, 2024

Nathan, that... was epic. LOL!

from libevhtp.

Turtleberry avatar Turtleberry commented on August 17, 2024

I'm going to be pushing up something more relevant here tonight, specifically I'm starting (and plausibly finishing)... hopefully, an abstracted logger with an interleaved ring buffer for performance and minimal locking...

from libevhtp.

NathanFrench avatar NathanFrench commented on August 17, 2024

Nice! I've been thinking about doing one of those for a while!

Ages ago, we wrote something to do this on another project:
https://github.com/mandiant/RProxy/blob/feature/ratelimiting/src/logger.c

The idea was to use {xxx} for specific parts of the request. The container that had multiple types of inputs/outputs (even Apple's stupid ASL):

https://github.com/mandiant/RProxy/blob/feature/ratelimiting/src/lzlog.c

Both of these are pretty terrible (I mean, if()s instead switches??), but it's a start.

from libevhtp.

NathanFrench avatar NathanFrench commented on August 17, 2024

Now back to the task at hand. As I was going through things, there are mixed (builtin so -NDEBUG does not remove it) assertions and normal checks.

I'm a fan of "fail close", but I don't know. I overlooked them since there was a lot of things I would have to figure out to free again. Which is why I wrote this header-only transparent reference counter:

#pragma once

#include <pthread.h>

static void (* REF_free)(void *) = free;
static void * (* REF_realloc)(void *, size_t) = realloc;
static void * (* REF_malloc)(size_t) = malloc;

struct refcount_ {
    pthread_mutex_t mux;
    unsigned int    count;
    char            data[];
};

#define ref_upcast(DAT) \
    (struct refcount_ *)((char *)(DAT - offsetof(struct refcount_, data)))

#define ref_barrier(CODE)           \
    pthread_mutex_lock(&refc->mux); \
    CODE                            \
    pthread_mutex_unlock(&refc->mux)


static inline void
ref_init_functions(void *(*mallocf)(size_t),
                   void * (*callocf)(size_t, size_t),
                   void *(*reallocf)(void *, size_t),
                   void (* freef)(void *))
{
    REF_malloc  = mallocf;
    REF_realloc = reallocf;
    REF_free    = freef;
}

static unsigned int
ref_inc(void * buf)
{
    struct refcount_ * refc = ref_upcast(buf);
    unsigned int       refs;

    ref_barrier({ refs = ++refc->count; });

    return refs;
}

static unsigned int
ref_dec(void * buf)
{
    struct refcount_ * refc = ref_upcast(buf);
    unsigned int       refs;

    ref_barrier({ refc->count -= 1; refs = refc->count; });

    return refs;
}

static inline void *
ref_malloc(size_t size)
{
    struct refcount_  * refc;
    pthread_mutexattr_t attr;

    refc        = REF_malloc(sizeof(*refc) + size);
    refc->count = 1;

    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(&refc->mux, &attr);

    return refc->data;
}

static void
ref_free(void * buf)
{
    struct refcount_ * refc = ref_upcast(buf);

    ref_barrier({
        if (--refc->count == 0)
        {
            pthread_mutex_unlock(&refc->mux);
            return REF_free(refc);
        }
    });
}

If we use that, it will be easy to know when to free and not by checking the refcount.

from libevhtp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.