Re: ERR_PTR usage question

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

 



On Tue, Jan 09, 2007 at 19:14:10 +0300, Tomasz Kvarsin wrote:
> Let's assume that I write function that will return
> struct buffer_head *, and want also signal about error
> can I use ERR_PTR mechanizm for this?
> 
> And more general question, when ERR_PTR bad decision,
> and when not?

If I recall correctly (someone please correct me if not -- I only used it in
2.4), the virtual addresses at the very end will never be allocated by
kmem_cache_alloc (or kmalloc, which is the same) and can therefore be used to
represent small integeral error codes, namely 1 to 999. Errno constants are
known to statisfy that restriction. So if you want to return buffer_head or
errno value, you can use ERR_PTR. For other kinds of error values it depends
whether they are small enough.

> I see some places in kernel, with such functions:
> 
> struct foo *f(int *err)
> {
> *err = ESOMERR;
> return NULL;
> }
> 
> why so?

Either because the pointer is not necessarily pointer to kernel internal
structure, or, more likely, because whoever wrote simply didn't realize he
could use the ERR_PTR trick. In fact I rarely saw such trick outside of linux
kernel, though small positive numbers would work well in userland (in fact
I think "small positive integers" are required to be invalid pointers so that
((struct whatever *)NULL).member is caught).

-- 
						 Jan 'Bulb' Hudec <bulb@xxxxxx>

Attachment: signature.asc
Description: Digital signature


[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