From: Ulrich Eckhardt on
io_x wrote:
> so this is the correct way to use it:
> #include <windows.h>
> #include <stdio.h>
> #include <stdlib.h>
> // #include <winbase.h> // don't compile
>
> CRITICAL_SECTION GlobalCriticalSection;
>
> void* malloc_r(unsigned s)
> {void *r;
> EnterCriticalSection(&GlobalCriticalSection);
> r=malloc(s);
> LeaveCriticalSection(&GlobalCriticalSection);
> return r;
> }
>
> void free_r(void* p)
> {EnterCriticalSection(&GlobalCriticalSection);
> free(p);
> LeaveCriticalSection(&GlobalCriticalSection);
> }
>
> int main(void)
> {void *r;
> unsigned i;
>
> InitializeCriticalSection(&GlobalCriticalSection);
>
> for(i=0; i<50000000; ++i)
> {r=malloc_r(500);
> free_r(r);
> }
> DeleteCriticalSection(&GlobalCriticalSection);
> return 0;
> }

Yes, this code should do what you want now.

Other than that, someone already mentioned that any halfway modern
malloc/free implementation already does that under the hood though,
so you are probably wasting your time.

Lastly, one more thing: malloc() takes a 'size_t' as parameter. This
makes code more portable, because it automatically becomes 64 bit
when on a 64 bit system.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

From: io_x on

"Ulrich Eckhardt" <eckhardt(a)satorlaser.com> ha scritto nel messaggio
news:a9g3a7-j3t.ln1(a)satorlaser.homedns.org...
> io_x wrote:
>> CRITICAL_SECTION GlobalCriticalSection;
>>
>> void* malloc_r(unsigned s)
>> {void *r;
>> InitializeCriticalSection(&GlobalCriticalSection);
>
> You initialize the CS exactly once, what you want here is
> EnterCriticalSection instead.

yes yes
thanks
so this is the correct way to use it:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
// #include <winbase.h> // don't compile

CRITICAL_SECTION GlobalCriticalSection;

void* malloc_r(unsigned s)
{void *r;
EnterCriticalSection(&GlobalCriticalSection);
r=malloc(s);
LeaveCriticalSection(&GlobalCriticalSection);
return r;
}

void free_r(void* p)
{EnterCriticalSection(&GlobalCriticalSection);
free(p);
LeaveCriticalSection(&GlobalCriticalSection);
}

int main(void)
{void *r;
unsigned i;

InitializeCriticalSection(&GlobalCriticalSection);

for(i=0; i<50000000; ++i)
{r=malloc_r(500);
free_r(r);
}
DeleteCriticalSection(&GlobalCriticalSection);
return 0;
}


> Uli
>
> --
> Sator Laser GmbH
> Gesch�ftsf�hrer: Thorsten F�cking, Amtsgericht Hamburg HR B62 932



From: Ulrich Eckhardt on
io_x wrote:
> "Dee Earley" <dee.earley(a)icode.co.uk> ha scritto nel messaggio
> news:4bcd7dff$0$346$7b0f0fd3(a)reader.news.newnet.co.uk...
>> On 20/04/2010 10:50, io_x wrote:
>>> How you from win32 can lock one resource used from many treadhs?
>>> example:
>>
>> CriticalSections maybe?
>
> maybe not, the last time i use it in my home made malloc() function
> with only one thread
> the program increase the memory in function of the time until sys slow
> down or seg fault if i remember well

win32's CRITICAL_SECTIONs do what you asked for, provided they are used
correctly. Show some faulty code and people could be able to tell you how
to fix it.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

From: io_x on

"Ulrich Eckhardt" <eckhardt(a)satorlaser.com> ha scritto nel messaggio
news:62n2a7-van.ln1(a)satorlaser.homedns.org...
> io_x wrote:
>> "Dee Earley" <dee.earley(a)icode.co.uk> ha scritto nel messaggio
>> news:4bcd7dff$0$346$7b0f0fd3(a)reader.news.newnet.co.uk...
>>> On 20/04/2010 10:50, io_x wrote:
>>>> How you from win32 can lock one resource used from many treadhs?
>>>> example:
>>>
>>> CriticalSections maybe?
>>
>> maybe not, the last time i use it in my home made malloc() function
>> with only one thread
>> the program increase the memory in function of the time until sys slow
>> down or seg fault if i remember well
>
> win32's CRITICAL_SECTIONs do what you asked for, provided they are used
> correctly. Show some faulty code and people could be able to tell you how
> to fix it.
ok
pheraps i don't understand the usage
but here this program it gets memory 0-20-40-180 meg etc etc
if i not break it <CONTROL-C> the program, always more memory

it will be malloc broken?
i think i make wrong something...
-------------
// i use Borland compiler
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
// #include <winbase.h> // don't compile what is the header?

CRITICAL_SECTION GlobalCriticalSection;

void* malloc_r(unsigned s)
{void *r;
InitializeCriticalSection(&GlobalCriticalSection);
r=malloc(s);
LeaveCriticalSection(&GlobalCriticalSection);
return r;
}

void free_r(void* p)
{InitializeCriticalSection(&GlobalCriticalSection);
free(p);
LeaveCriticalSection(&GlobalCriticalSection);
}

int main(void)
{void *r;
unsigned i;

for(i=0; i<50000000; ++i)
{r=malloc_r(500);
free_r(r);
}
return 0;
}
---

> Uli
>
> --
> Sator Laser GmbH
> Gesch�ftsf�hrer: Thorsten F�cking, Amtsgericht Hamburg HR B62 932
>




From: Ulrich Eckhardt on
io_x wrote:
> CRITICAL_SECTION GlobalCriticalSection;
>
> void* malloc_r(unsigned s)
> {void *r;
> InitializeCriticalSection(&GlobalCriticalSection);

You initialize the CS exactly once, what you want here is
EnterCriticalSection instead.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932