473,738 Members | 4,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

my little implementation of malloc

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

/*************** **********CCCCC CC************* *************** ******/
#ifdef __cplusplus
extern "C" {
#endif

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

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

void* realloc_sys(voi d *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[i]=p2[i];
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_ro utines");) */

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(size of(vettore_arra y));
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_arr ay**) 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(size of(vettore_arra y));
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(unsign ed nbytes)
{Header *p, *prevp;
unsigned nunits, *k, *kk, led, size;
double dnm;
Header* morecore(unsign ed);
unsigned verifica_all_m( void );
void stato_mem(void) ;
/*-------------------------------*/
if(libero_m_)
{if(verifica_al l_m()==0) stato_mem();}

if(i_i_i_>MEM_L IMIT || 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(H eader) >sizeof(unsigne d) ) ? 1: 0) + 1;
if((prevp=freep )==NULL) /* non esiste la free list */
{ base.s.ptr=free p=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_m MEM_+ 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_m MEM_- 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_al l_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_mME M_+ 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_mME M_- 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(voi d)
{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(voi d)
{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(unsign ed 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(lis t_, 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[i]=p2[i];
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_[i]=nam[j];
F( ; j< 15; ++i, ++j)
name_[i]=' ';
name_[i] = '#';
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(cha r* 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_al l_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_ );}

Jan 27 '07
52 3388
On 27 Jan 2007 00:48:14 -0800, in comp.lang.c , "vi****@gmail.c om"
<vi****@gmail.c omwrote:

(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
Jan 27 '07 #21
On Sat, 27 Jan 2007 15:54:21 +0100, in comp.lang.c , jacob navia
<ja***@jacob.re mcomp.frwrote:
>I agree with that. How can one write an allocator
without calling the OS?
You probably can't. The point is that it was offtopic in both CLC and
CLA since it is by its nature highly platform specific.

Moreover the style was highly reminiscent of LaCrilola or whatever the
troll was called who enjoys messing with CLC, CLC++ and CLA in similar
fashion. The fact that the comments are in italian is substantive.
--
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
Jan 27 '07 #22
jacob navia wrote:
vi****@gmail.co m a écrit :
>>
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
I am not an expert in 'c' but then how does malloc allocate memory
independent of different systems then?
this discussion is confusing me!
Is is not possible to write an allocator which is independent of the
architecture and the OS?? i doubt it
Jan 27 '07 #23
¬a\/b wrote:

#define P printf
#define W while
#define F for
#define R return
Back again with a new ID? Well, *plonk* again as well.

Brian
Jan 27 '07 #24
Kelly wrote:
jacob navia wrote:
vi****@gmail.co m a écrit :
>
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

I am not an expert in 'c' but then how does malloc allocate memory
independent of different systems then?
Generally no, it can't. The implementation of malloc() for a
particular system, does call the system's API to get memory.
this discussion is confusing me!
Is is not possible to write an allocator which is independent of the
architecture and the OS?? i doubt it
Independant of architecture, I doubt that. Independant of the OS,
quite possible, but that's not the case with most desktop and
mainframe computers.

malloc() is in almost all cases, very system dependant, which is why,
among other reasons, ab's post is not topical to clc. It would've for
example been topical in comp.os.ms-windows.win32.p rogrammer. OTOH,
generic allocation strategies are topical to comp.programmin g.

Jan 27 '07 #25
Robert Redelmeier <re****@ev1.net .invalidwrites:
In alt.lang.asm vi****@gmail.co m <vi****@gmail.c omwrote 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.
malloc(), as far as the C language is concerned, is simply a standard
function provided by the implementation. The standard doesn't mention
syscalls; malloc() could theoretically be implemented as a syscall, as
an ordinary function call, or as a message sent by carrier pigeon.
(It probably happens to be a function call that may use syscalls
internally on most systems.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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.
Jan 27 '07 #26
hey, don't be nazi.
OK, you've lost the argument according to standard Usenet etiquette.
wow, didn't think this word will "generate" so much reactions. what i
meant: don't follow rules too strict beyond their purpose. simple
phrase.
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.
most assembly programers i know use win as their workstations. btw, I
count people, not number of their machines.
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 am pretty new here, no idea. but i would like to test it, i have
been testing my own heap manager recently, and it's no fun to catch
some errors.
Depending on your definition of "calling the OS" I can think of three ways.
sure, i meant more like "using OS-specific interface"
Allocate memory from a large static array.
i meant, real-world usable general heap manager of course...

Jan 27 '07 #27
vi****@gmail.co m wrote, On 27/01/07 21:54:

Please leave in the attribution lines. The little bits that say who
posted what. It makes it easier to follow the conversation.
>>>hey, don't be nazi.
OK, you've lost the argument according to standard Usenet etiquette.
wow, didn't think this word will "generate" so much reactions. what i
meant: don't follow rules too strict beyond their purpose. simple
phrase.
Doesn't matter. The rules are the rules. Anyway, the rules have been
developed for good reasons.
>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.
most assembly programers i know use win as their workstations. btw, I
count people, not number of their machines.
Then you only know a small proportion of assembler programmers. My
experience is that most of them are working on embedded systems using
processors that would not run any version of 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 am pretty new here, no idea. but i would like to test it, i have
been testing my own heap manager recently, and it's no fun to catch
some errors.
Test it if you wish, but would you really want to use C code that the C
exports say is bad and from a known troll?
>Depending on your definition of "calling the OS" I can think of three ways.
sure, i meant more like "using OS-specific interface"
Then it belongs on an OS-specific group. Surprising that OS-specifics
belong on OS-specific groups, isn't it ;-)

Well, alt.lang.asm might be OK as well for all I know, but comp.lang.c
definitely is *not* the right place.
>Allocate memory from a large static array.
i meant, real-world usable general heap manager of course...
That *is* a real-world used heap management strategy, and used for very
good reasons.
--
Flash Gordon
Jan 27 '07 #28
On 27 Jan 2007 13:54:06 -0800, in comp.lang.c , "vi****@gmail.c om"
<vi****@gmail.c omwrote:
>most assembly programers i know use win as their workstations.
What, including all the ones programming for Unix, Linux, VMS, MVS ??
Shurely Shome Mishtake.
>Allocate memory from a large static array.
>i meant, real-world usable general heap manager of course...
This _is_ a realworld usable method. Think about what the OS itself
does...
--
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
Jan 28 '07 #29
"vi****@gmail.c om" <vi****@gmail.c omécrivait news:1169934846 .792436.4580
@k78g2000cwa.go oglegroups.com:
most assembly programers i know use win as their workstations.
Therefore your interrest with the "portabilit y" mythology,
must be...

:)

Betov.

< http://rosasm.org >

Jan 28 '07 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2793
by: Ruslan Vorobey | last post by:
Hi All. Does anybody saw any C++ new operator implementations? I need just to know does it use malloc? Especially I interesting about standard C++ libs implementation under AIX 5.1L. Thank you in advance.
12
7602
by: Anil | last post by:
Hi, Where can I get the source code that has the implementation of the below C operators/functions: malloc sizeof new strcmp strtok
8
1696
by: farhanb | last post by:
hello, I am writing a simple linked list implementation. When I use function insert1 I must allocate to head in my main to get it to work otherwise list stays empty but when I use function insert2 there is no need to allocate to head this is the correct way. why does this happen? why do we need to have a pointer to a pointer to head in the insert function for this to work?
14
1852
by: Henk | last post by:
Hi Guys, (see actual code below) I wrote a little program that is supposed to read a file (input.txt) into memory (using a stack) and then reverse the file and display it to output. It works, but I still get some errors when i try to compile it: stack.c: In function 'reverseFile'
49
23175
by: Karsten Jung | last post by:
Hello together, We have to make our own implementation of malloc() under Linux. Does anybody can give me a hint where to start? Our implementation should only work under Linux. Thanks Karsten
41
3344
by: jacob navia | last post by:
In the C tutorial for lcc-win32, I have a small chapter about a debugging implementation of malloc. Here is the code, and the explanations that go with it. I would appreciate your feedback both about the code and the associated explanations. ---------------------------------------------------------------------
14
1382
by: Army1987 | last post by:
#include <string.h> #include <stdlib.h> #include <stdio.h> char *_(char *str1, char *str2) { char *res; res = malloc((strlen(str1)+strlen(str2))*sizeof(char)); strcpy(res, str1); strcpy(res+strlen(str1), str2); return res;
27
5544
by: Kislay | last post by:
How is realloc implemented internally ? If there is not enough memory in place to allocate , is new memory allocated somewhere else and the 2 regions linked via a pointer , OR , is the old region copied to a place where there is enough memory to fit both old and new region ? In the second case , wouldn't this method be inefficient if the old region was large ?
3
3518
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument passed to the operators is calculated correctly during runtime and passed in. Inside the implementation both operators (new and new) do a simple malloc(). Ditto for operator delete/delete. I have two questions: 1) Who passes in the size_t argument...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.