[haiku-gsoc] Re: Implementation Queries

  • From: Adrien Destugues <pulkomandy@xxxxxxxxxxxxx>
  • To: haiku-gsoc@xxxxxxxxxxxxx
  • Date: Wed, 28 Jun 2017 07:35:21 +0200

On Wed, Jun 28, 2017 at 05:30:58AM +0530, Vivek Roy wrote:

Okay, on further searching I came across pcmcia/k_compat.h
<http://xref.plausible.coop/source/xref/haiku/headers/os/drivers/pcmcia/k_compat.h>

It reads:
/* Memory allocation.  BeOS doesn't have an atomic malloc. */
#define kmalloc(s,f)        malloc(s)
#define kfree(p)        free(p)
#define kfree_s(p,s)        free(p)
void *malloc();
void free(void *);

So I should ignore all flags and just do a malloc(sizeof(kobj)) ?

It depends which flags are used. You didn't tell us which flags kmalloc
is using in your use cases.

If you don't know which flags are used, I would start with something like this:

void* kmalloc(size_t size, uint32_t flags)
{
        if (flags & CONTIGUOUS_MEMORY)
                PANIC("kmalloc doesn't know how to do contiguous memory yet!");

        if (flags & ...)
                ...

        // ok, nothing special required. few!
        return malloc(size);
}


On Wed, Jun 28, 2017 at 4:42 AM, Vivek Roy <vivekroyandroid@xxxxxxxxx>
wrote:

Okay, so I was trying to allocate physically non-contiguous memory. I was
looking at Kernel Kit's create_area() to be used in place of kzalloc.

A few things I noted (please let me know if I am wrong):
1. A call to kzalloc expects the memory to be zero-ed out. create_area
doesn't have any such flag so it has to be done by making a call to memset
(We can use memset on the memory returned from create_area right?)

You can use memset, or calloc.

2. I am using the B_ANY_KERNEL_ADDRESS constant for addr_spec parameter of
create_area

Yes, if there are no other constraints on the address, this is fine.

3. The size alllocated has to be a multiple of B_PAGE_SIZE. So for
allocating sizeof(kobj) bytes, I will have to allocate [(sizeof(kobj) + 
B_PAGE_SIZE
- 1) / B_PAGE_SIZE] bytes of memory.

I think create_area would take care of this already? Anyway, if
something request a kmalloc of 3 bytes, certainly it shouldn't use a
full B_PAGE_SIZE and just use 3 bytes.

4. For locking constant I think B_LAZY_LOCK should be okay
5. Protection is B_READ_AREA | B_WRITE_AREA

In general, create_area is dedicated to allocating pages of memory. It
is a rather low level operation. This is not always needed, if kmalloc
is used just to allocate some general purmose memory, then a simple
malloc in Haiku would probably do. And creating a dedicated area (of at
least 4 kilobytes) for each allocation, no matter how small, would waste
quite a lot of memory in the end.

I expect you can find if create_area is needed depending on the kmalloc
flags. But, you didn't list which flags are used in your case, so I
don't know which function is best used.

-- 
Adrien.

Other related posts: