my little implementation of malloc

Discussion in 'C Programming' started by ¬a\\/b, Jan 27, 2007.

  1. ¬a\\/b

    ¬a\\/b Guest

    is it possible that a
    malloc function that use a nobody student
    is better than all yours :)

    How many errors do you see?

    Bug1: memory<13MB
    Bug2:
    it seems do you have to write you own sprintf "Ps_m"
    Ps_m(char* out, int len, char* fmt, ...)
    that has the len of array in the second arg

    #include <windows.h>
    #include <winbase.h>
    #include <wincon.h>
    #include <math.h>
    #include <float.h>
    #include <limits.h>
    #include <winuser.h>

    #include "winb.h"
    #define IVA_ INVALID_HANDLE_VALUE

    #define P printf
    #define W while
    #define F for
    #define R return

    /* MEM_LIMIT e' il limite del numero di diversi puntatori
    ritornati da malloc_m */
    #define MEM_LIMIT 200000
    #define MAX_ARRAYS 255

    /*************************CCCCCCC**********************************/
    #ifdef __cplusplus
    extern "C" {
    #endif

    void* malloc_sys(int n)
    {if(n<0) R 0;
    R (void*)LocalAlloc(LMEM_FIXED, n);
    }

    void free_sys(void* p){ LocalFree(p); }

    void* realloc_sys(void *p, int nbytes)
    {int k;
    uns a, r, j, i;
    char *p1, *p2;

    if(nbytes<0) R 0;
    k=LocalSize(p);
    if(k<0) R 0;
    p1= (char*) LocalAlloc(LMEM_FIXED, nbytes);
    if(p1==0) R 0;
    i = k<=nbytes? k: nbytes;
    a=i/(sizeof(int)); r=i&(sizeof(int)-1);
    F(j=0, p2=(char*)p; j<a; ++j)
    ((int*)p1)[j]=((int*)p2)[j];
    F(i=4*j, r+=i; i<r; ++i)
    p1=p2;
    LocalFree(p);
    R p1;
    }

    #define NALLOC 1024
    // 1024 /* numero minimo di unita' richieste */
    // 1024*256*8=1024*1024*2 = 2 mega

    static unsigned yuyuy=0;
    typedef double Align; /* per allineare alla double */

    union header { /* header del blocco */
    struct {union header* ptr; /* blocco successivo in free list */
    unsigned size; /* dimensione di questo blocco */
    }s;
    Align x; /* forza l'allineamento dei blocchi */
    }; /* sizeof header == 4+4 = 8 */

    typedef union header Header;
    static Header base; /* lista vuota per iniziare */
    static Header *freep=NULL; /* inizio della free list */

    static char name_ [ 1100 ] = {0}; /* name_== nome routines
    chiamate */
    static char memo_ [ 300 ] = {0}; /* stringa ove errori
    trovati in memoria */
    static int n_i_ = 0 ;
    int libero_m_ = 0 ; /* libero_m_==1 controllo tutti i
    vettori a
    ogni chiamate di free(); serve insieme a name_ per individuare le
    routines che
    sovrascrivono la memoria che non hanno (e.g. if(libero_m_)
    init_w("nome_routines");) */

    typedef struct {
    void *p;
    unsigned size;
    unsigned piu;
    unsigned meno;
    }vettore_array;

    static vettore_array **p_p_p_=0; /* lista di struct di puntatori
    ritornati da malloc_m */
    static int i_i_i_=0; /* attuale indice nella lista */
    static int m_m_m_=0; /* massimo indice nella lista */
    static char **list_ =0; /* lista dei vettori richiesti al
    sys */
    static int list_i_=0;
    static int list_m_=0;
    static int show_si =0;

    static int mem_init(int siz)
    {vettore_array **pp;
    int j;
    /*---------------------*/
    //P("p_p_p_=%p i_i_i_==%u siz=%u ", (void*) p_p_p_, i_i_i_, siz );
    if(siz<0) R 0;
    if(p_p_p_==0)
    {pp= (vettore_array**) malloc_sys( (siz+1) * sizeof *pp );
    if(pp==0) R 0;
    F( j=0; j<siz; ++j)
    {pp[j]= (vettore_array*) malloc_sys(sizeof(vettore_array));
    if(pp[j]==0)
    {if(j!=0)
    F( --j ; j!=0 ; --j)
    free_sys( (void*) pp[j] );
    free_sys((void*) pp[0]); free_sys((void*) pp); R 0;
    }
    }
    show_si=0;
    }
    else { if(siz==m_m_m_) R 1;
    else if(siz < m_m_m_) /* dovrebbe distruggere la
    memoria eccessiva */
    { if(siz<=i_i_i_) R 1; /* la 1024ma volta che viene
    chiamato free_m()*/
    F(j=siz; j<m_m_m_; ++j) // v[0]...v[size]=size+1
    free_sys((void*) p_p_p_[j]);
    pp=(vettore_array**) realloc_sys(p_p_p_, (siz + 1) *
    sizeof *pp );
    if(pp==0) {P("Fallimento realloc\n"); m_m_m_ = siz; R
    1;}
    /* ritorna senza cambiare niente */
    goto label;
    }
    pp= (vettore_array**) realloc_sys( (void*) p_p_p_, (siz + 1)
    * sizeof *pp);
    if(pp==0) {P("Fallimento realloc\n"); R 0;}
    if( siz > i_i_i_)
    { F( j=i_i_i_; j<siz; ++j)
    {
    pp[j]= (vettore_array*) malloc_sys(sizeof(vettore_array));
    if(pp[j]==0)
    {if(j!=0)
    F( --j ; j!=0 ; --j)
    free_sys( (void*) pp[j] );
    free_sys((void*) pp[0]); R 0;
    }

    }
    }
    }
    label: ;
    m_m_m_ = siz; p_p_p_=pp; R 1;
    }


    /* malloc_m: allocatore di memoria */
    void* malloc_m(unsigned nbytes)
    {Header *p, *prevp;
    unsigned nunits, *k, *kk, led, size;
    double dnm;
    Header* morecore(unsigned);
    unsigned verifica_all_m( void );
    void stato_mem(void);
    /*-------------------------------*/
    if(libero_m_)
    {if(verifica_all_m()==0) stato_mem();}

    if(i_i_i_>MEM_LIMIT || nbytes==0 ) R 0;
    if(i_i_i_>=m_m_m_)
    {if(mem_init( i_i_i_ + MAX_ARRAYS ) == 0) R 0;}
    nunits = nbytes + sizeof(Header);
    nunits = nunits/sizeof(Header) +
    (( nunits%sizeof(Header) >sizeof(unsigned) ) ? 1: 0) + 1;
    if((prevp=freep)==NULL) /* non esiste la free list */
    { base.s.ptr=freep=prevp=&base; base.s.size=0; }
    for(p=prevp->s.ptr; ; prevp=p, p=p->s.ptr)
    {if(p->s.size>=nunits) /* spazio insufficiente */
    {if(p->s.size==nunits) /* esattamente */
    {prevp->s.ptr = p->s.ptr;}
    else /* alloca la parte finale */
    {p->s.size -= nunits;
    p += p->s.size;
    p->s.size =nunits;
    }
    freep = prevp;
    /****************** Blocco aggiunto ************/
    size = nbytes;
    led = (size % sizeof(unsigned)!=0) ? 1: 0;
    k = (unsigned*)(p+1); kk=k; --k;
    kk[size/sizeof(unsigned)+ led]=(uns)(p+1);
    p_p_p_[i_i_i_]->meno = *k;
    p_p_p_[i_i_i_]->piu = kk[size/sizeof(unsigned)+ led];
    p_p_p_[i_i_i_]->size = size;
    p_p_p_[i_i_i_++]->p = (void*)(p+1);
    /***********************************************/
    return (void*) (p+1);
    }
    if(p==freep) /* la free list e' terminata */
    { // ho aggiunto le seguenti 11 righe
    dnm = yuyuy + (nunits<NALLOC ? NALLOC: nunits);
    dnm = (dnm *8.0)/1024000.0; // tutta la memoria in MB
    if(dnm > 13.1 ) G lab90; // se memoria > 13.1MB esci
    if((p=morecore(nunits)) == NULL)
    {
    lab90:;
    if(stdout_m!=0 && stdout_m->fd!=(int)IVA_)
    P("\nErrore: memoria insufficiente\n");
    exit_m(1); // voglio uscire
    return NULL; /* non c'e' piu' spazio */
    }
    yuyuy += (nunits<NALLOC ? NALLOC: nunits);
    /* numero di unita' di mem. richieste al sistema */
    }
    }
    }


    /*
    unsigned verifica_all_m( void )
    Ritorna il numero degli elementi;
    se il numero degli elementi e' 0 ritorna (unsigned) -1
    se errori di out of bound per qualche puntatore ritorna 0
    */
    unsigned verifica_all_m( void )
    {unsigned *k, *kk, xk, xkk, led;
    int j;
    /*----------------*/
    if( p_p_p_==0 || i_i_i_==0 ) R -1;
    F(j=0 ; j < i_i_i_ ; ++j)
    {k = (unsigned*) (p_p_p_[j]->p); kk=k; --k;
    led = (p_p_p_[j]->size % sizeof(unsigned)!=0) ? 1: 0;
    if( (xk=kk[p_p_p_[j]->size/sizeof(unsigned) + led]) !=
    (xkk=p_p_p_[j]->piu) )
    { Ps_m(memo_ , 300, "verifica_all_mMEM_+ p=%p size=%u
    p[-1]=%u p[max]=%u n:v=%u:%u",
    p_p_p_[j]->p, p_p_p_[j]->size, p_p_p_[j]->meno,
    xkk, xk, xkk);
    if(n_i_>2) name_[ n_i_ - 2 ]='+';
    return 0;
    }
    if( (xk=*k) != (xkk=p_p_p_[j]->meno) )
    { Ps_m(memo_ , 300, "verifica_all_mMEM_- p=%p size=%u
    p[-1]=%u p[max]=%u n:v=%u:%u",
    p_p_p_[j]->p, p_p_p_[j]->size, xkk,
    p_p_p_[j]->piu, xk, xkk);
    if(n_i_>2) name_[n_i_ - 2]='-';
    return 0;
    }
    }
    R i_i_i_;
    }

    void verifica_all( void )
    {if(verifica_all_m( )==0)
    {P("Errore nella memoria\n");
    if(*memo_ !=0 && *name_ != 0 )
    P("memo=%s name=%s\n", memo_, name_ );
    }
    }

    /* unsigned verifica_m( void* pointer, int *jj)
    -se pointer==0 ritorna
    1 se tutto e' cancellato
    2 se tutto e' cancellato tranne il puntatore iniziale
    3 altrimenti
    -se pointer!=0 ritorna
    0 se il vettore puntato da pointer fa parte della lista
    1 se tutto e' cancellato
    7 se il vettore puntato da pointer non fa parte della lista
    8 se il vettore puntato da pointer fa parte della lista
    ma ci sono errori di scrittura "out of bounds" 'iniziali'
    9 se il vettore puntato da pointer fa parte della lista
    ma ci sono errori di scrittura "out of bounds" 'finali'
    */
    unsigned verifica_m( void* pointer, int *jj)
    {unsigned *k, *kk, xk, xkk, led;
    int j;
    /*----------------*/
    if(jj!=0) *jj=0;
    if(pointer==0)
    {if( i_i_i_==0 && p_p_p_==0 ) R 1;
    else if(i_i_i_==0) R 2;
    else R 3;
    }
    if(p_p_p_==0 ) R 1;

    F(j=i_i_i_-1 ; j>=0 ; --j)
    if( p_p_p_[j]->p == pointer ) break;

    if(j<0) {P("\nverifica_mMEM_0 0x%p indice=%u ", pointer, i_i_i_ );
    if(*memo_ && *name_ )
    P( "memo=%s name=%s", memo_, name_ );
    P("\n");
    if(n_i_>2) name_[n_i_ -2]='0';
    R 7;
    }
    k = (unsigned*) pointer; kk=k; --k;
    led = (p_p_p_[j]->size % sizeof(unsigned)!=0) ? 1: 0;
    *jj=j;
    if( (xk=kk[p_p_p_[j]->size/sizeof(unsigned) + led]) !=
    (xkk=p_p_p_[j]->piu) )
    { P("\np=0x%p size=%u p[-1]=%u p[max]=%u ", pointer,
    p_p_p_[j]->size, *k, xk);
    P("verifica_mMEM_+ n:v>%u:%u\n", xk, xkk);
    if(n_i_>2) name_[ n_i_ - 2 ]='+';
    return 8;
    }
    if( (xk=*k) != (xkk=p_p_p_[j]->meno) )
    { P("\np=0x%p size=%u p[-1]=%u p[max]=%u ", pointer,
    p_p_p_[j]->size, xkk, p_p_p_[j]->piu);
    P("verifica_mMEM_- n:v>%u:%u\n", xk, xkk);
    if(n_i_>2) name_[n_i_ - 2]='-';
    return 9;
    }
    else R 0;
    }

    unsigned verifica_p_m( void* pointer)
    {int jj; R verifica_m( pointer, &jj);}

    static void free_list_m(void)
    {W(list_i_!=0)
    {--list_i_; free_sys(list_[list_i_]);}
    free_sys(list_); list_=0; list_m_=0; freep=NULL;
    }

    static void free_vettore_m(void)
    {int j;
    if(p_p_p_==0) { m_m_m_=0; i_i_i_=0; R;}
    F(j=0; j<m_m_m_; ++j) // v[0]...v[size]=size+1
    free_sys((void*) p_p_p_[j]);
    free_sys((void*) p_p_p_);
    p_p_p_=0; m_m_m_=0; i_i_i_=0;
    }


    static void inserisci(void* ap, unsigned jk, int j)
    {Header *bp, *p, *r;
    static unsigned f_cont=0;
    /*-------------------*/
    if(jk==0) ++f_cont;
    bp = (Header*)ap - 1; /* punta all'header del blocco */
    F( r=p=freep; !(bp>p && bp < p->s.ptr); r=p, p=p->s.ptr )
    if( p >= p->s.ptr && (bp>p || bp < p->s.ptr) )
    break; /* il blocco liberato e'
    ad un estremo della lista*/
    if(jk) goto label;
    /********************************************/
    if(j != i_i_i_ - 1)
    {p_p_p_[j]->p = p_p_p_[i_i_i_ - 1]->p;
    p_p_p_[j]->size = p_p_p_[i_i_i_ - 1]->size;
    p_p_p_[j]->meno = p_p_p_[i_i_i_ - 1]->meno;
    p_p_p_[j]->piu = p_p_p_[i_i_i_ - 1]->piu;
    }
    p_p_p_[--i_i_i_]->p =0; p_p_p_[i_i_i_]->size=0;
    p_p_p_[i_i_i_]->meno=0; p_p_p_[i_i_i_]->piu =0;
    if(m_m_m_>1000 && f_cont%1024==0)
    mem_init(i_i_i_+64);
    /***********************************************/
    label:
    if(bp+bp->s.size == p->s.ptr)/*lo unisce al blocco dopo*/
    {bp->s.size += p->s.ptr->s.size;
    bp->s.ptr = p->s.ptr->s.ptr;
    }
    else bp->s.ptr=p->s.ptr;

    if(p+p->s.size == bp)/* lo unisce al blocco prima*/
    {p->s.size += bp->s.size;
    p->s.ptr = bp->s.ptr;
    freep=r; R;
    }
    else p->s.ptr=bp;
    freep = p;
    }



    void free_all_m(void) {free_vettore_m(); free_list_m();}

    /*Non usare free_all_m() ma free_a_i_m se ci sono oggetti
    allocati da costruttori e distruttori del c++
    (gli oggetti fuori da una qualsiasi procedura finiscono quando si
    chiude
    l'ultima istruzione del main) */
    void free_a_i_mm(void)
    {if(i_i_i_==0)
    {free_vettore_m(); free_list_m();
    if(stdout_m->flag)
    P("MEMORIA DINAMICA LIBERATA Tot=%0f Mb\n",
    ((yuyuy*8.0)/1024000.0) );
    }
    else if( i_i_i_>=1 && p_p_p_!= 0 )
    {if(stdout_m->flag)
    P("Vi e' ancora da liberare 0x%p\n", (void*) p_p_p_[0]->p);
    }
    }

    void free_a_i_m(void)
    {if(i_i_i_==0 && show_si==0)
    {free_vettore_m(); free_list_m();
    if(stdout_m->flag)
    P("MEMORIA DINAMICA LIBERATA Tot=%.4L Mb\n",
    (long double) ((yuyuy*8.0)/1024000.0) );
    show_si=1;
    }
    }


    void free_a_r_m(void)
    {if(i_i_i_==0)
    {free_vettore_m(); free_list_m(); }
    else if( i_i_i_>=1 && p_p_p_!= 0 && stdout_m->flag!=0)
    {P("Vi e' ancora da liberare 0x%p\n", (void*)
    p_p_p_[0]->p);}
    }

    void free_a_e_m(void)
    {if(i_i_i_==0 && p_p_p_!=0)
    {free_vettore_m(); free_list_m();
    if(stdout_m->flag && p_p_p_==0)
    {stato_mem();
    P("Memoria libera\n");
    }
    }
    else if( i_i_i_>=1 && p_p_p_!= 0 && stdout_m->flag)
    {P("Vi e' ancora da liberare 0x%p\n", (void*)
    p_p_p_[0]->p);}
    }


    /* morecore: chiede al sistema memoria aggiuntiva */
    static Header* morecore(unsigned nu)
    {char *cp, **p;
    Header *up;
    unsigned len;
    /*-----------------------*/
    if(nu<NALLOC) nu=NALLOC;
    // P("@@");
    cp = (char*) malloc_sys(nu * sizeof(Header));
    // P("%u##", nu);
    if( cp==0 ) /* non c'e' piu' spazio */
    return NULL;

    if(list_i_ >= list_m_ )
    {len = (list_m_==0 ? 128: 2*list_m_);
    if(list_==0) list_ = (char**) malloc_sys(4);
    p = (char**) realloc_sys(list_, len * sizeof *p);
    if(p==0) {free_sys(cp); return 0;}
    list_ = p;
    list_m_ =len;
    }
    list_[list_i_++]=cp;
    up=(Header*) cp;
    up->s.size=nu;
    inserisci((void*)(up+1), 1, 0);
    return freep;
    }

    /* se pointer!=0 ritorna la size del pointer
    ritorna 0 se il pointer non c'e' */
    unsigned get_size_m( void* pointer )
    {int j;
    /*----------------*/
    if(pointer==0 || p_p_p_==0) R 0;
    F(j=i_i_i_-1 ; j>=0 ; --j)
    if( p_p_p_[j]->p == pointer ) break;
    if(j<0) R 0;
    R p_p_p_[j]->size;
    }

    void* realloc_m1(void *p, unsigned nbytes)
    {unsigned j, k;
    char *p1, *p2=(char*)p;
    p1= (char*) malloc_m(nbytes);
    if(p1==0) R 0;
    k=get_size_m(p);
    k = (k<=nbytes? k:nbytes);
    F( j=0; j<k; ++j)
    p1[j]=p2[j];
    free_m(p);
    R p1;
    }

    void* realloc_m(void *p, int nbytes)
    {int k;
    uns a, r, j, i;
    char *p1, *p2;
    if(nbytes<0) R 0;
    k=get_size_m(p);
    if(k<0) R 0;
    p1= (char*) malloc_m(nbytes);
    if(p1==0) R 0;
    i = k<=nbytes? k: nbytes;
    a=i/(sizeof(int)); r=i&(sizeof(int)-1);
    F(j=0, p2=(char*)p; j<a; ++j)
    ((int*)p1)[j]=((int*)p2)[j];
    F(i=4*j, r+=i; i<r; ++i)
    p1=p2;
    free_m(p);
    R p1;
    }

    void init_w(const char *nam)
    {unsigned i, j;
    /*---------------------*/
    if(nam==0) R;
    if( n_i_ > 2 )
    name_[n_i_ - 1]='|';
    if( n_i_ >= 1016 ) // 0..14 carattere 15='|'
    n_i_=0;
    F(i=n_i_, j=0; j<15 && nam[j]!=0; ++i, ++j)
    name_=nam[j];
    F( ; j< 15; ++i, ++j)
    name_=' ';
    name_ = '#';
    n_i_ += 16;
    }

    void leggi_mem(void)
    {Header *u, *v;
    unsigned j;
    F( u = freep; u!=0 ; )
    {P("|v=%u s=%u|", (unsigned)u / sizeof(Header), u->s.size );
    if(u->s.ptr==freep) break;
    v=u->s.ptr;
    if((u + u->s.size) != v)
    {j = (uns)(v-u) - u->s.size;
    if(j<123456)
    P("%u", j );
    else P("#");
    }
    u=v;
    }
    }

    void stato_mem(void)
    {unsigned j;
    leggi_mem();
    P("\nRAPPORTO MEMORIA DINAMICA. ");
    P("Memoria richiesta al sistema=%.4f Mb ", ((yuyuy*8.0)/1024000.0) );
    j=verifica_all_m();
    if(j!=0 && j!=-1)
    {P("%u vettor%c allocat%c con successo\n",
    j, j==1?'e':'i', j==1?'o':'i');
    if( p_p_p_!= 0 && p_p_p_[0]!=0 )
    {P("Vi e' ancora da liberare 0x%p\n", (void*)
    p_p_p_[0]->p);}
    }
    else {if(j==0) {P("Out of bounds "); goto label;}
    P("Il sistema non ha vettori da liberare ritornati da
    malloc_m\n"); // j==-1
    j=verifica_m(0, 0);
    if(j==1) P("Sia la base sia il contatore sono NULLI\n");
    else if(j==2) P("Il contatore e' nullo ma la base no\n");
    else {label: ;
    P("Errori nella memoria\n");
    if(*memo_ && *name_)
    P("memo=%s name=%s\n", memo_, name_ );
    // free_vettore_m(); free_list_m();
    exit_m(1);
    }
    }
    }

    void controlla_m(char* st)
    {P("!%s!", st);
    if(verifica_all_m()==0)
    {if(st) P("ERRORE!");
    stato_mem();
    }
    }


    /* free: inserisce in free list il blocco ap */
    void free_m(void* ap)
    { int j, k;
    /*-------------------*/
    if(ap==0) R ;
    if(libero_m_)
    {if(verifica_all_m()==0)
    stato_mem();
    }
    if(k=verifica_m(ap, &j))
    { if(k==7)
    {P("free_m(): ha cercato di liberare un puntatore che
    non \n");
    P("appartiene a quelli rilasciati da malloc_m: Esco...
    \n");
    }
    else if(k==8 || k==9)
    {P("free_m(): ha cercato di liberare un puntatore
    \n");
    P("in cui \"out of bound\" riscrittura. Esco... \n");
    }
    stato_mem();
    // free_vettore_m(); free_list_m();
    exit_m(1);
    }
    inserisci(ap, 0, j);
    }

    void stampa_chiamate(void){P("memo=%s name=%s\n", memo_, name_ );}
    ¬a\\/b, Jan 27, 2007
    #1
    1. Advertising

  2. ¬a\\/b

    ¬a\\/b Guest

    i would sai it is better
    malloc(int a) than malloc(size_t a) or malloc(unsigned a)
    because i can control if a<0 and in that case exit
    ¬a\\/b, Jan 27, 2007
    #2
    1. Advertising

  3. ¬a\/b wrote:
    > is it possible that a
    > malloc function that use a nobody student
    > is better than all yours :)
    >
    > How many errors do you see?


    Before we get into any substantive code,

    > #include <windows.h>
    > #include <winbase.h>
    > #include <wincon.h>
    > #include <winuser.h>
    > #include "winb.h"


    All of the above are non-standard. Anything in the code that depends on
    any of the above will not work outside of one specific proprietary
    system. Post to a newsgroup for that OS, not here, but first check
    their FAQs and prior postings. Your post may be beyond the size anyone
    bothers with.


    > #define P printf
    > #define W while
    > #define F for
    > #define R return


    To avoid being thought an illiterate fool, get rid of the above.
    Martin Ambuhl, Jan 27, 2007
    #3
  4. På Sat, 27 Jan 2007 07:26:28 -0000, skrev ¬a\/b <>:

    > is it possible that a
    > malloc function that use a nobody student
    > is better than all yours :)


    Much likly. This seems to me like _typical_ c.
    C is used by most programmers, so it must be the best?
    Therefore your code must be best.
    I dont know c, so I cant confirm.

    :)
    //\\\\o//\\\\annabee, Jan 27, 2007
    #4
  5. ¬a\\/b

    Ico Guest

    In comp.lang.c "¬a\\/b" <> wrote:
    > is it possible that a
    > malloc function that use a nobody student
    > is better than all yours :)
    >
    > How many errors do you see?


    I didn't even bother to look, since my compiler found 46 already.

    [snipped IOCCC-worthy code]

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
    Ico, Jan 27, 2007
    #5
  6. ¬a\\/b

    Guest

    > i would sai it is better
    > malloc(int a) than malloc(size_t a) or malloc(unsigned a)
    > because i can control if a<0 and in that case exit

    not really. malloc with unsigned argument can have some upper-bound
    defined (other than 7FFFFFFFh), and result is same.
    , Jan 27, 2007
    #6
  7. ¬a\\/b

    Guest

    > > #include <windows.h>
    > > #include <winbase.h>
    > > #include <wincon.h>
    > > #include <winuser.h>
    > > #include "winb.h"

    > All of the above are non-standard. Anything in the code that depends on
    > any of the above will not work outside of one specific proprietary
    > system. Post to a newsgroup for that OS, not here, but first check
    > their FAQs and prior postings. Your post may be beyond the size anyone
    > bothers with.

    hey, don't be nazi. you NEED to call system to do memory management.
    Point of this topic is to demonstrate his handler, and it is impossible
    without. And it's enough to support one OS in demonstration, win32 is a
    good choice because most asmers use it.

    > > #define P printf
    > > #define W while
    > > #define F for
    > > #define R return

    oh god... don't be afraid of typing. also choose less obfuscative names
    for your variables
    , Jan 27, 2007
    #7
  8. ¬a\\/b

    Kelly Guest

    "����������������" wrote:
    > is it possible that a
    > malloc function that use a nobody student
    > is better than all yours :)
    >
    > How many errors do you see?


    My compiler shows 51 errors 17 warnings(is this a code :p) apart from
    the fact that all the headers are non-standard...so system specific..not
    portable!
    Kelly, Jan 27, 2007
    #8
  9. Martin Ambuhl <> writes:
    > ¬a\/b wrote:

    [snip]
    > > #define P printf
    > > #define W while
    > > #define F for
    > > #define R return

    >
    > To avoid being thought an illiterate fool, get rid of the above.


    We've been telling him that for years; it hasn't done any good.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jan 27, 2007
    #9
  10. ¬a\\/b

    santosh Guest

    ¬a\/b wrote:
    > is it possible that a
    > malloc function that use a nobody student
    > is better than all yours :)
    >
    > How many errors do you see?

    <sniped code eligible for ioccc.org>

    This group (comp.lang.c), doesn't discuss details of implementation.
    Since you appear to be targetting the Windows platform, post to a
    Windows group. It's also not topical for alt.lang.asm.
    santosh, Jan 27, 2007
    #10
  11. ¬a\\/b

    santosh Guest

    //\\\\o//\\\\annabee wrote:
    > På Sat, 27 Jan 2007 07:26:28 -0000, skrev ¬a\/b <>:
    >
    > > is it possible that a
    > > malloc function that use a nobody student
    > > is better than all yours :)

    >
    > Much likly. This seems to me like _typical_ c.


    No it's not. avb is well known in all the groups he posts in, for using
    illegible style and needlessly obfuscating his code. This is not an
    example of good C programming.
    santosh, Jan 27, 2007
    #11
  12. In alt.lang.asm <> wrote in part:
    > hey, don't be nazi. you NEED to call system to do memory management.


    At some level, yes. The traditional one is brk() and mmap()
    has recently been added to most OSes. malloc() itself is _NOT_
    a syscall but very deliberately a libc call.

    At a more detailed level, users ought to write their own
    allocators when they have specialised needs that potentially
    aren't handled by the general purpose malloc().

    -- Robert
    Robert Redelmeier, Jan 27, 2007
    #12
  13. ¬a\\/b

    jacob navia Guest

    a écrit :
    >>>#include <windows.h>
    >>>#include <winbase.h>
    >>>#include <wincon.h>
    >>>#include <winuser.h>
    >>>#include "winb.h"

    >>
    >>All of the above are non-standard. Anything in the code that depends on
    >>any of the above will not work outside of one specific proprietary
    >>system. Post to a newsgroup for that OS, not here, but first check
    >>their FAQs and prior postings. Your post may be beyond the size anyone
    >>bothers with.

    >
    > hey, don't be nazi. you NEED to call system to do memory management.
    > Point of this topic is to demonstrate his handler, and it is impossible
    > without. And it's enough to support one OS in demonstration, win32 is a
    > good choice because most asmers use it.
    >


    I agree with that. How can one write an allocator
    without calling the OS?

    jacob
    jacob navia, Jan 27, 2007
    #13
  14. ¬a\\/b

    santosh Guest

    jacob navia wrote:
    > a écrit :
    > >>>#include <windows.h>
    > >>>#include <winbase.h>
    > >>>#include <wincon.h>
    > >>>#include <winuser.h>
    > >>>#include "winb.h"
    > >>
    > >>All of the above are non-standard. Anything in the code that depends on
    > >>any of the above will not work outside of one specific proprietary
    > >>system. Post to a newsgroup for that OS, not here, but first check
    > >>their FAQs and prior postings. Your post may be beyond the size anyone
    > >>bothers with.

    > >
    > > hey, don't be nazi. you NEED to call system to do memory management.
    > > Point of this topic is to demonstrate his handler, and it is impossible
    > > without. And it's enough to support one OS in demonstration, win32 is a
    > > good choice because most asmers use it.
    > >

    >
    > I agree with that. How can one write an allocator
    > without calling the OS?


    To be pedantic an allocator's job is to manage a chunk of memory.
    Nothing says that that chunk should be created from a syscall. A
    simplistic allocator can be built around a global array for example.
    santosh, Jan 27, 2007
    #14
  15. ¬a\\/b

    Flash Gordon Guest

    jacob navia wrote, On 27/01/07 14:54:
    > a écrit :
    >>>> #include <windows.h>
    >>>> #include <winbase.h>
    >>>> #include <wincon.h>
    >>>> #include <winuser.h>
    >>>> #include "winb.h"
    >>>
    >>> All of the above are non-standard. Anything in the code that depends on
    >>> any of the above will not work outside of one specific proprietary
    >>> system. Post to a newsgroup for that OS, not here, but first check
    >>> their FAQs and prior postings. Your post may be beyond the size anyone
    >>> bothers with.

    >>
    >> hey, don't be nazi.


    OK, you've lost the argument according to standard Usenet etiquette.

    >> you NEED to call system to do memory management.
    >> Point of this topic is to demonstrate his handler, and it is impossible
    >> without. And it's enough to support one OS in demonstration, win32 is a
    >> good choice because most asmers use it.


    I've got no idea what you mean by "asmers", but a lot of people don't
    use Windows here. Of the half dozen machines I've been using whilst
    working today, or example, *none* are running Windows.

    In any case, based on what others have spotted in the code it looks
    suspiciously like the return of a troll who has been here before, in
    which case the the purpose is not to demonstrate anything, it is merely
    to annoy.

    > I agree with that. How can one write an allocator
    > without calling the OS?


    Depending on your definition of "calling the OS" I can think of three ways.

    Use the C library provided malloc and friends. Yes, people do sometimes
    do this to an extent for perfectly good reasons, and discussion of such
    would be perfectly topical here.

    Write an OS where malloc and friends are the primitive memory management
    functions, then it is OS code rather than calling the OS. How to do this
    would not be topical here.

    Allocate memory from a large static array. There is no standard way to
    ensure you have met the alignment requirements for all types, but other
    than that it is all standard C and discussion of it would be topical of
    it here. There are times when there are good reasons for doing this.
    --
    Flash Gordon
    Flash Gordon, Jan 27, 2007
    #15
  16. In alt.lang.asm "?a\\/b" <> wrote in part:
    > is it possible that a malloc function that use a nobody
    > student is better than all yours :)


    Poxy `c` is OT to the point of flamebait for ALA.

    "better" is a relative term that evokes some sort of
    standard and values. ASM has no std malloc() and you've
    given no hint of what values you code is good at.

    -- Robert
    Robert Redelmeier, Jan 27, 2007
    #16
  17. wrote:
    >>> #include <windows.h>
    >>> #include <winbase.h>
    >>> #include <wincon.h>
    >>> #include <winuser.h>
    >>> #include "winb.h"

    >> All of the above are non-standard. Anything in the code that depends on
    >> any of the above will not work outside of one specific proprietary
    >> system. Post to a newsgroup for that OS, not here, but first check
    >> their FAQs and prior postings. Your post may be beyond the size anyone
    >> bothers with.

    > hey, don't be nazi. you NEED to call system to do memory management.


    Don't engage in gratuitous name calling. If you need system calls, then
    you have no business posting to comp.lang.c. Such posts belong in
    system-specific newsgroups, as I already pointed out.
    Martin Ambuhl, Jan 27, 2007
    #17
  18. ¬a\\/b

    Betov Guest

    Flash Gordon <> écrivait news:vh7s84xvv7.ln2
    @news.flash-gordon.me.uk:

    > I've got no idea what you mean by "asmers"



    May i suggest... sodomists?

    :)

    Betov.

    < http://rosasm.org >
    Betov, Jan 27, 2007
    #18
  19. In article <45bb679d$0$5099$>,
    jacob navia <> wrote:
    >I agree with that. How can one write an allocator
    >without calling the OS?


    An OS could provide heap space without system calls just as it
    provides stack space.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Jan 27, 2007
    #19
  20. On 27 Jan 2007 00:48:14 -0800, in comp.lang.c , ""
    <> wrote:

    (of someone posting platform-specific nonstandard code to CLC)

    >hey, don't be nazi.


    Godwins rule?

    >you NEED to call system to do memory management.


    Sure. but then its offtopic in CLC, whose topic is portable code, and
    its offtopic in CLA because its not assembler.

    >> > #define P printf
    >> > #define W while
    >> > #define F for
    >> > #define R return

    >oh god... don't be afraid of typing. also choose less obfuscative names
    >for your variables


    For your reference, the guy who posts using these abbreviations is (or
    is copying) a known troll.

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Jan 27, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    681
  2. ravi
    Replies:
    0
    Views:
    436
  3. Peter
    Replies:
    34
    Views:
    1,898
    Richard Tobin
    Oct 22, 2004
  4. ThaDoctor
    Replies:
    3
    Views:
    369
    Alan Woodland
    Sep 28, 2007
  5. Daniel
    Replies:
    1
    Views:
    196
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page