Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

New Cancer - Femgineers

Name: Anonymous 2012-06-08 14:20

Name: Anonymous 2012-06-12 9:19

>>120
Indeed, all the printf functions will ordinarily use malloc themselves. >>119 needs to use fputs.

Name: Anonymous 2012-06-12 9:37

>>121
Yep. vsnprintf() and vsprintf() don't allocate their own buffers, but all the others do.

fprintf generally looks something like this:
size_t fprintf(FILE* stream, const char* fmt, ...)
{
    char* buffer;
    va_list ap;
    va_start(ap, fmt);
    count = vsnprintf(NULL, 0, fmt, ap);
    buffer = malloc(count);
    count = vsnprintf(buffer, count, fmt, ap);
    va_end(ap);
    fputs(buffer, stream);
    free(buffer);
    return count;
}

or something along those lines.

Name: Anonymous 2012-06-12 12:00

>>120
>>121
>>122
nice input gentleman, thank you

Name: Anonymous 2012-06-12 14:33

>>123
>>120-122
Please to optimize your references.

Name: Anonymous 2012-06-12 15:47

I wrote my own C heap allocator. It probably doesn't compile because I didn't try to compile it or even briefly check for syntax errors, and even when it does compile it probably won't work without some [b][i][o][u]REFACTORING[/i][/o][/i][/b] but I'm not submitting this as an attempt to win a degree, so here it is:

heapofshit.h
#ifndef HEAP_OF_SHIT_H
#define HEAP_OF_SHIT_H

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
# ifndef WINDOWS
#  define WINDOWS
# endif
#elif defined(POSIX) || defined(__POSIX__) || defined(unix) || defined(__unix__)
# ifndef POSIX
#  define POSIX
# endif
#endif

/**
 * \brief    Allocates a block of shit.
 * \param size    The size of the shit-block in bytes.
 * \return    On success, a pointer to the shit-block is returned. On error,
 *        NULL is returned and errno is set to indicate the error that
 *        occurred.
 */
void* take_shit(size_t size);

/**
 * \brief    Allocates a block of shit and sets every
 * \param count    The number of elements in the shit-block.
 * \param size    The size in bytes of each shit-element.
 * \return    On success, a pointer to the shit-block is returned. On error,
 *        NULL is returned and errno is set to indicate the error that
 *        occurred.
 */
void* ztake_shit(size_t count, size_t size);

/**
 * \brief    Resizes a block of shit.
 * \param shit    A pointer to the shit-block to be resized.
 * \param new_size The new size of the shit-block.
 * \return    On success, the resized shit-block is returned. On error, NULL
 *        is returned and errno is set to indicate the error that
 *        occurred.
 */
void* resize_shit(void* shit, size_t new_size);

/**
 * \brief    Flushes a block of shit.
 * \param shit    The shit-block to flush. If this is a null pointer, no action is
 *        performed.
 * \return    This function does not return a value.
 */
void flush_shit(void* shit);

#endif /* ! HEAP_OF_SHIT_H */


heapofshit.c
#include <heapofshit.h>

#if defined(WINDOWS)
# include <Windows.h>
#elif defined(POSIX)
# include <unistd.h>
#endif

#ifdef POSIX
/** Stores shit-blocks. */
static struct shitblock {
    void*    start;    /**< The start of the shit-block. */
    size_t    size;    /**< The size of the shit-block. */
    int    used;    /**< Whether the shit-block is in use. */
} * shitblocklist;    /**< The list of shit-blocks. */
size_t num_shitblocks;    /**< The number of shit-blocks in the list. */
ssize_t next_free = -1;    /**< Index of the next free block. */
#endif

/**
 * \brief    Allocates a block of shit.
 * \param size    The size of the shit-block in bytes.
 * \return    On success, a pointer to the shit-block is returned. On error,
 *        NULL is returned.
 */
void* take_shit(size_t size)
{
#if defined(WINDOWS)
    return HeapAlloc(GetProcessHeap(), 0, size);
#elif defined(POSIX)
    if (!(shitblocklist)) {
        /* Initialise the shit-block list.
         */
        shitblocklist = sbrk(sizeof(*shitblocklist) * (num_shitblocks + 1));
        shitblocklist[0].start = sbrk(size);
        shitblocklist[0].size  = size;
        shitblocklist[0].used  = 1;
        ++num_shitblocks;
        return shitblocklist[0].start;
    } else if ((next_free >= 0) && (size <= shitblocklist[next_free].size)) {
        /* Use the next free block if it's big enough.
         */
        void* p = shitblocklist[next_free].start;
        shitblocklist[next_free].used = 1;
        next_free = -1;
        /* Cba to set next_free to the next free block because I'm /hc/.
         *Besides that, it'd defeat the point of having it (which was so
         * we wouldn't have to search for free blocks immediately after
         * a free).
         */
        return p;
    } else {
        /* Look for a big enough free block.
         */
        void* p;
        size_t i;
        for (i = 0; i < num_shitblocks; ++i) {
            if ((shitblocklist[i].used == 0) && (shitblocklist[i].size >= size)) {
                shitblocklist[i].used = 1;
                return shitblocklist[i].start;
            }
        }
        /* If we haven't returned yet we need to take a new shit.
         */
        p = sbrk(sizeof(*shitblocklist) * (num_shitblocks + 1));
        if (!(p))
            return NULL;
        /* Copy the old list and add the new shitblock.
         */
        memcpy(p, shitblocklist, sizeof(*shitblocklist) * (num_shitblocks + 1));
        shitblocklist[num_shitblocks].start = sbrk(size);
        shitblocklist[num_shitblocks].size  = size;
        shitblocklist[num_shitblocks].used  = 1;
    }
    return NULL;
#endif
    /* OH FUCK LOOK AT ALL THAT HEAP ALLOCATION I DID MAN THAT WAS NICE. */
}

/**
 * \brief    Allocates a block of shit and sets every
 * \param count    The number of elements in the shit-block.
 * \param size    The size in bytes of each shit-element.
 * \return    On success, a pointer to the shit-block is returned. On error,
 *        NULL is returned.
 */
void* ztake_shit(size_t count, size_t size)
{
#if defined(WINDOWS)
    return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
#elif defined(POSIX)
    void* shit = take_shit(count * size);
    if (shit)
        memset(shit, 0, count * size);
    return shit;
#endif
}

/**
 * \brief    Resizes a block of shit.
 * \param shit    A pointer to the shit-block to be resized.
 * \param new_size The new size of the shit-block.
 * \return    On success, the resized shit-block is returned. On error, NULL
 *        is returned.
 */
void* resize_shit(void* shit, size_t new_size)
{
#if defined(WINDOWS)
    return HeapReAlloc(GetProcessHeap(), shit, new_size);
#elif defined(POSIX)
    void* new_shit = take_shit(new_size);
    if (new_shit) {
        memcpy(new_shit, shit, size);
        flush_shit(shit);
    }
    return new_shit;
#endif
}

/**
 * \brief    Flushes a block of shit.
 * \param shit    The shit-block to flush. If this is a null pointer, no action is
 *        performed.
 */
void flush_shit(void* shit)
{
    if (!(shit))
        return;
#ifdef WINDOWS
    HeapFree(GetProcessHeap(), 0, shit);
#elif defined(POSIX)
    size_t i;
    if (sbrk(0) == shit) {
        /* Give memory at the end of the program-break back to the OS.
         */
        for (i = 0; i < num_shitblocks; ++i) {
            if (shitblocklist[i].start == shit) {
                sbrk(0 - shitblocklist[i].size);
                return;
            }
        }
    } else {
        /* Mark the block free so take_shit() can use it later.
         */
        for (i = 0; i < num_shitblocks; ++i) {
            if (shitblocklist[i].start == shit) {
                shitblocklist[i].used = 0;
                return;
            }
        }
    }
#endif
}


I spent, maybe, 30 minutes on this?

Name: Anonymous 2012-06-12 15:51

>>125
This should go after line 70:
free(shitblocklist);
shitblocklist = p;

Also the formatting is because 4chan replaces tabs with spaces (rhymes).

Name: Anonymous 2012-06-12 15:58

>>124
references
back to sepples, please

Name: Anonymous 2012-06-12 17:06

I keep confusing that word with femniggers.

>>125
I think you are leaking space when you move the shitblocklist. Also, you can't use neither free() nor flush_shit(shitblocklist).

You didn't pass.

Name: Anonymous 2012-06-12 18:13

I hate most feminists because they are illogical pieces of shit.

Name: Anonymous 2012-06-12 19:03

its because they lack a penis

Name: Anonymous 2012-06-13 3:46

>>109
First, you're sorely mistaken if you think /proggles/ hasn't hated feminists for a long time.
But I don't give a fuck about that. I'm more bitter than ever and I hate you. Fuck you.

Name: Anonymous 2012-06-13 11:51

>>128
Yeah, you're right. Like I said, I spent 30 minutes or so on it.

Name: VIPPER 2012-06-13 13:17

I hate most humans because they are illogical pieces of shit.

Name: Anonymous 2012-06-13 15:14

>>130
Penis is a vital part of every programmer.

I cannot imagine programming without my penis.

Name: Anonymous 2012-06-13 18:52

>>134
Same here: what would I use to press the keys?

Name: Anonymous 2012-06-14 1:54

>>135
ONE WORD: /THE FORCED DICKING OF THE KEYS, THREAD OVER!!!!!!!!!!!/

Name: Anonymous 2012-06-14 4:33

>>136
don't use sage when shitposting

Name: Anonymous 2012-06-14 10:36

>>137
Get a load of this guy acting as if getting past his shitty filter isn't the point of saging while shitposting.

Name: Anonymous 2012-06-14 16:58

>>138
I don't use a filter.  I am the master shitposter.

Name: Anonymous 2012-12-27 10:39

>>19
Non sequitur. Consider this: A pack of wild niggers human behaviour is programmed by the environment (i.e. early childhood). If you spend your childhood in a mathless computerless environment, you won't just "grow" an interest or passion for mathematics out of nothing. A blank brain seldom comes up with stuff just on its own. The programming of human infants by their early environment explains a lot in society, e.g. illogical traditions and social standards, organized religion, why society is shit, and much more. It is therefore not a stretch to imagine that most females are programmed by their early environment (by noticing the status quo and trying to fit in) to be "artsy", to act "girlish", to spend time in front of the mirror applying face-paint, and to bear a distaste for the pure sciences.

Gender is shit.

Name: Anonymous 2012-12-27 11:41

>>21
That was VIP quality!

Name: Anonymous 2012-12-27 13:43


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List