#ifdef for 32-bit platform

后端 未结 11 1424
眼角桃花
眼角桃花 2020-12-30 00:37

In an application I maintain, we\'ve encountered a problem with file descriptor limitations affecting the stdlib. This problem only affects the 32-bit version of the standar

相关标签:
11条回答
  • 2020-12-30 01:04

    At least 32-bit Solaris has a limit of 256 file pointers because the structure stores the file descriptor in an unsigned char field. This is retained for backwards compatibility with some almost impossibly old versions of SunOS. Other platforms - I'm tempted to say most other platforms - do not share that limitation. On the other hand, it is relatively unusual for an ordinary user program to need that many files open concurrently; it more often indicates a bug (not closing the files when finished with them) than not. Having said that, though, it can be a problem for things like database servers which need to have lots of data files open at the same time.


    One comment says:

    That's almost it. We don't have a large number of files open, but the server handles a large number of connections from clients. Socket handles and file descriptors seem to come from the same place. When we have a lot of connections, 'fopen' fails because the system-level call returns and fd > 255.

    'Socket handles' are file descriptors at the system call level, so they come from the same place as regular file descriptors for files.

    If you have to work around this, then you need to wrap your current socket opening code so that if it gets an file descriptor in the range 0..255, then it calls 'dup2()' to create a file descriptor in the range that stdio won't use - and then close the original file descriptor. The only snag with this is that you have to keep track of which file descriptors are available, because dup2 will merrily close the target file descriptor if it is currently open.

    Of course, I'm assuming your socket code reads file descriptors and not file pointers. If that's the case, you have bigger problems - too many things want to use the same resources and they can't all use them at the same time.

    0 讨论(0)
  • 2020-12-30 01:05

    I believe the define is _WIN64

    0 讨论(0)
  • 2020-12-30 01:05

    There is no such symbol defined by the C++ standard - your specific platform (which you haven't specified in your question) may provide one.

    0 讨论(0)
  • 2020-12-30 01:07

    I'm not sure if there is a universal #if def that is appropriate. The C++ standard almost certainly does not define one. There are certainly platform spcefic ones though.

    For example, Windows

    #if _WIN64 
    // 64 bit build
    #else
    // 32 bit build
    #endif
    

    EDIT OP mentioned this is a cross compile between Windows and Non-Windows using GCC and other compilers

    There is no universal macro that can be used for all platforms and compilers. A little bit of preprocessor magic though can do the trick. Assuming you're only working on x86 and amd64 chips the following should do the trick. It can easily be expanded for other platforms though

    #if _WIN64 || __amd64__
    #define PORTABLE_64_BIT
    #else
    #define PORTABLE_32_BIT
    #endif
    
    0 讨论(0)
  • 2020-12-30 01:09

    I use a construction like this for Windows:

    #if defined(_WIN64)
       //64-bit code
    #elif  defined(_M_IX86)
       //32-bit code
    #else
    #error "Unknown platform"
    #endif
    

    Versus:

    #if defined(_WIN64)
      // 64 bit code
    #else
      // 32-bit code
    #endif
    

    In the former solution, because of the #error the compiler will be able to tell you where you need to add code for a new platform. This aids maintainability should you ever encounter a platform that is neither 64-bit nor 32-bit. Yes, the _M_IX86 is not exactly synonymous with 32-bit, but I think the only 32-bit platform most of us are supporting is in fact x86. So as a practical measure it suffices.

    In the later solution you'll have to manually figure out where you need code for your new platform using grep or something like that. This is tedious and error prone.

    It occurs to me that the following construction would also be acceptable, although I have not tested it in production nor I have really thought about it very much.

    #if defined(_WIN64)
       //64-bit code
    #elif  defined(_WIN32)
       //32-bit code
    #else
    #error "Unknown platform"
    #endif
    
    0 讨论(0)
提交回复
热议问题