Team BBL
Previous Page Next Page

Chapter 2

2.1

The following technique is used by FreeBSD. The primitive data types that can appear in multiple headers are defined in the header <machine/_types.h>.

For example:

    #ifndef _MACHINE__TYPES_H_
    #define _MACHINE__TYPES_H_

    typedef int            __int32_t;
    typedef unsigned int   __uint32_t;
    ...

    typedef __uint32_t     __size_t;
    ...

    #endif  /* _MACHINE__TYPES_H_ */

In each of the headers that can define the size_t primitive system data type, we have the sequence

    #ifndef _SIZE_T_DECLARED
    typedef __size_t      size_t;
    #define _SIZE_T_DECLARED
    #endif

This way, the typedef for size_t is executed only once.

2.3

If OPEN_MAX is indeterminite or ridiculously large (i.e., equal to LONG_MAX), we can use geTRlimit to get the per process maximum for open file descriptors. Since the per process limit can be modified, we can't cache the value obtained from the previous call (it might have changed). See Figure C.1.

Figure C.1. Alternate method for identifying the largest possible file descriptor
#include "apue.h"
#include <limits.h>
#include <sys/resource.h>

#define OPEN_MAX_GUESS 256

long
open_max(void)
{
    long openmax;
    struct rlimit rl;

    if ((openmax = sysconf(_SC_OPEN_MAX)) < 0 ||
      openmax == LONG_MAX) {
        if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
            err_sys("can't get file limit");
        if (rl.rlim_max == RLIM_INFINITY)
            openmax = OPEN_MAX_GUESS;
        else
            openmax = rl.rlim_max;
    }
    return(openmax);
}

    Team BBL
    Previous Page Next Page