Re: allocating aligned memory

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



looking into kernel source, it seemed quite simple:

./sound/pci/emu10k1/memory.c:
#define get_aligned_page(offset)    ((offset) >> PAGE_SHIFT)
    blk->first_page = get_aligned_page(blk->mem.offset);
    blk->last_page = get_aligned_page(blk->mem.offset + blk->mem.size - 1);
    psize = get_aligned_page(size + PAGE_SIZE -1);
    page = get_aligned_page(offset);

./sound/pci/trident/trident_memory.c:
#define get_aligned_page(offset)    ((offset) >> 12)
#define get_aligned_page(offset)    ((offset) >> 13)
#define get_aligned_page(offset)    ((offset) / ALIGN_PAGE_SIZE)
    ptr = page_to_ptr(trident, get_aligned_page(offset));
    psize = get_aligned_page(size + ALIGN_PAGE_SIZE -1);

ie, just AND with the alignement requirement.

And the trident_memory.c:is_valid_page() function worked like this:

/*
 * check if the given pointer is valid for pages
 */
static int is_valid_page(unsigned long ptr)
{
        if (ptr & ~0x3fffffffUL) {
                snd_printk(KERN_ERR "max memory size is 1GB!!\n");
                return 0;
        }
        if (ptr & (SNDRV_TRIDENT_PAGE_SIZE-1)) {
                snd_printk(KERN_ERR "page is not aligned\n");
                return 0;
        }
        return 1;
}

So, as the last part shows, just AND the address with the alignment requirement will do.

But I have not looked else where.


On Tue, Feb 17, 2009 at 9:53 AM, Pei Lin <telent997@xxxxxxxxx> wrote:
>>And kmalloc is page aligned, "4096/16 = 256", is it also 16 aligned?

>>>Maybe i misunderstand about "aligned" meaning.

Oh,sorry vmalloc is page aligned kmalloc is not.


2009/2/17 Pei Lin <telent997@xxxxxxxxx>:
> i think slab can do that,look at
> mm/slab.c:
> "struct kmem_cache *
> kmem_cache_create (const char *name, size_t size, size_t align,
> unsigned long flags,
> void (*ctor)(struct kmem_cache *, void *))"
>
> the parameter "align" for what?
>
> And kmalloc is page aligned, "4096/16 = 256", is it also 16 aligned?
>
> Maybe i misunderstand about "aligned" meaning.
>
>
>
> 2009/2/16 Mulyadi Santosa <mulyadi.santosa@xxxxxxxxx>:
>> On Sun, Feb 15, 2009 at 4:41 PM, Ramagudi Naziir <naziirr@xxxxxxxxx> wrote:
>>> hello everyone
>>>
>>> i need to allocate memory aligned 16 bytes.
>>>
>>> how can i do that ?
>>>
>>> can I use kmalloc for that ?
>>
>> Uhm, kmalloc use slab so I don't think it will be aligned. My wild
>> guess is you have use alloc_page(s) or use _aligned_ (or _align_?...
>> not sure) gcc builtin directive
>>
>> regards,
>>
>> Mulyadi.
>>
>> --
>> To unsubscribe from this list: send an email with
>> "unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
>> Please read the FAQ at http://kernelnewbies.org/FAQ
>>
>>
>

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ




--
Regards,
Peter Teoh

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux