kcalloc equivalent

Hi Guys,

I’m trying to find the equivalent function of “kcalloc” in windows WDK KDMF.

Looking forward.

Zakaria.

ExAllocatePoolWithTag

Hi,

It’s not, in kcalloc, I could define the number of elements and their size ? here I could define the size of one element?

Any idea on how I work around this ?

Thanks a lot.

Zakaria

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xyz.sun.ok@163.com
Sent: 16 April 2015 10:45
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] kcalloc equivalent

ExAllocatePoolWithTag


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer


Micron Europe Limited
Registered in England, Company No. 02341071
Registered Office: L’Avenir, Opladen Way
Bracknell, Berkshire RG12 0PH, UK

To clarify more, I done what I’m trying to do in Linux C like the following kcalloc(Table.NumberOfStructures,sizeof(SMBIOSHeader)*Table.NumberOfStructures,GFP_KERNEL);

In Windows C I came up with this
ExAllocatePoolWithTag(NonPagedPool, sizeof(SMBIOSHeader)*Table.NumberOfStructures, “tables”);

but still massing the argument where I can define number of elements like an array ?

Why don’t you want just to turn yor brains on???

If you do you are going to realize that kcalloc(n,size,flags) is nothing more than just a wrapper for kmalloc(n*size,flags). If you still have any doubts about it you can check Linux sources.

Therefore, If you are just desperate for kcalloc(PoolType, n,size,tag) you can implement it as ExAllocatePoolWithTag(PoolType, n*size, tag). Simple, ugh…

Anton Bassov

Aha, thanks anton, I thought that it would work but the problem I’m getting compiler error of

Error 1 error C2220: warning treated as error - no ‘object’ file generated

from these lines,

INT i, k, amountofstrings = 0, charactercounter = 0, offset = 0;
physico.QuadPart = Table.TableAddress;
PUCHAR address = MmMapIoSpace(physico, Table.TableLength, 0);
HeadTypes = (SMBIOSHeader *)ExAllocatePoolWithTag(NonPagedPool, sizeof(SMBIOSHeader)*Table.NumberOfStructures, “tables”);

I can’t really find what’s wrong with ?

Thanks

Why don’t you just implement it yourself using ExAllocate function or look for Lookaside lists if you want something with fixed sized allocs.
But I guess asking this question in the first place is questionable :slight_smile:

if you have something like:
MY_ELEMENT_TYPE *memoToAlloc;
and you want a vector of n times this structure or whatever just do something like:

void *myWindowskcalloc(int numCoutofelements, int sizeofeachelement, int pooltag)
{
pvoid **vector = NULL;
vector = (void**)ExAllocatePoolWithTag(PagedPool, numberofelements* sizeof(PVOID), pooltag);
if (!vector ) return NULL;

for (i = 0; i < numCount; i++)
{
vector[i] = ExAllocatePoolWithTag(PagedPool, size, pooltag);
//check for error condition and cleanup if necessary the memory already allocated
RtlZeroMemory(vector[i], size); // to have it zeroed already

}

return vector;

}

After this you have your vector of n times the structure you want.
You also have to make you cleanup function or Free version of this.

Any warnings ?

Gabriel, I figured this out, the only thing was confusing me, is the memory alignment size in the pool, when I define the pool size then I want to store in several element of structure that has its size, how can I define this when I iterate through, yet I figured out that’s is all like this, since I never looked at kcalloc definition, I thought there is something else, so this for your " I guess asking this question in the first place is questionable " .

I already did this in the very previous comment you will find

To clarify more, I done what I’m trying to do in Linux C like the following
kcalloc(Table.NumberOfStructures,sizeof(SMBIOSHeader)*Table.NumberOfStructures,GF
P_KERNEL);

In Windows C I came up with this
ExAllocatePoolWithTag(NonPagedPool,
sizeof(SMBIOSHeader)*Table.NumberOfStructures, “tables”);

yet I’m getting this warning only

error C2220: warning treated as error - no ‘object’ file generated

The code is

INT32 i, k, amountofstrings = 0, charactercounter = 0, offset = 0;
physico.QuadPart = Table.TableAddress;
PUCHAR address = MmMapIoSpace(physico, Table.TableLength, 0);
HeadTypes = (SMBIOSHeader *)ExAllocatePoolWithTag(NonPagedPool, sizeof(SMBIOSHeader)*Table.NumberOfStructures, “tables”);

Any idea

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: 16 April 2015 11:30
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] kcalloc equivalent

Any warnings ?


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer


Micron Europe Limited
Registered in England, Company No. 02341071
Registered Office: L’Avenir, Opladen Way
Bracknell, Berkshire RG12 0PH, UK

call exallocatepoolwithtag with the last parameter like this
‘tab’ NOT “tables”
Check that

And btw when I meant warning there was for sure a warning saying something like
‘ExAllocatePoolWithTag’ : different types for formal and actual parameter 3

That what you printed was just the error.
Enable the build output to also show you warnings, and fix those too.
I also don’t understand this line:
INT32 offset =3D 0;

Good luck.

Sorry that mistake came here while I was writing the message,

Actually from the source code is
INT32 i, k, amountofstrings = 0, charactercounter = 0, offset = 0;

it’s just for me to iterate through the smbios table structures

Aha got you I found 11 warnings and gives more information

Very decent :slight_smile:

Thanks a lot

>offset =3D 0;

Seems to be a typo which, apparently, happens to be a showstopper…

Anton Bassov

My questions imply my windows drivers knowledge level, therefore I have to clarify that I’m an intern student at Micron, :slight_smile:

Also, thanks for your cooperation guys :slight_smile:

The tag argument of ExAllocatePoolWithTag is an ULONG, not char*.

Next time, you probably want to post from gmail and leave the name of your employer out of it.

Just some friendly advice,

Peter
OSR
@OSRDrivers

xxxxx@micron.com wrote:

My questions imply my windows drivers knowledge level, therefore I have to clarify that I’m an intern student at Micron, :slight_smile:

Let me point out as gently as I can that the questions you were asking
here were simple C questions, and were not driver-related in any way.
If you are really not comfortable with the basics of C yet, then it may
be too early for you to be digging in to kernel drivers.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

My advice is the opposite. Kudos on jumping right in to kernel development.
I was writing driver code long before I ever wrote my first line of
user-mode code. See if this helps:

PVOID ExCallocPool (POOL_TYPE pt, size_t num_elem, size_t size)
{
SIZE_T bytes = num_elem * size;
PVOID ptr = ExAllocatePool(pt, bytes);

if (ptr != NULL)
RtlZeroMemory(ptr, bytes);

return ptr;
}

I have not tested this, it is just off the top of my head.

On Thu, Apr 16, 2015 at 12:47 PM, Tim Roberts wrote:

> xxxxx@micron.com wrote:
> > My questions imply my windows drivers knowledge level, therefore I have
> to clarify that I’m an intern student at Micron, :slight_smile:
>
> Let me point out as gently as I can that the questions you were asking
> here were simple C questions, and were not driver-related in any way.
> If you are really not comfortable with the basics of C yet, then it may
> be too early for you to be digging in to kernel drivers.
>
> –
> Tim Roberts, xxxxx@probo.com
> Providenza & Boekelheide, Inc.
>
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>


Jamey Kirby
Disrupting the establishment since 1964

This is a personal email account and as such, emails are not subject to
archiving. Nothing else really matters.

OR

And I’ll be in the middle: It doesn’t matter whether you learn to write C in user-mode or kernel-mode, but you need to learn how to write C.

Mr. Roberts was right when he said “the questions you were asking here were simple C questions” and we’re probably not the most, ahem, patient people on the face of the planet, ahem, with such questions.

For the record, I’m like Mr. Kirby: I never wrote a line of user-mode C code until I had written probably a dozen kernel-mode drivers. It took me years to develop any sort of appreciation whatsoever for user-mode code development. In fact, some might say I’ve yet to develop an adequate appreciation.

Peter
OSR
@OSRDrivers