summaryrefslogtreecommitdiff
path: root/lib/util.h
Commit message (Collapse)AuthorAgeFilesLines
...
* lib: Move compiler.h to <openvswitch/compiler.h>Thomas Graf2014-12-151-16/+16
| | | | | | | | | | The following macros are renamed to avoid conflicts with other headers: * WARN_UNUSED_RESULT to OVS_WARN_UNUSED_RESULT * PRINTF_FORMAT to OVS_PRINTF_FORMAT * NO_RETURN to OVS_NO_RETURN Signed-off-by: Thomas Graf <tgraf@noironetworks.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: Fix include for htonl().Joe Stringer2014-12-041-1/+1
| | | | | | | | | | Commit 526a7c85d11dc "util: Add be32_prefix_mask()." added an include for byte-order.h into util.h, which could cause link failures if users of libopenvswitch defined their own version of htonll(). Change the include, as only htonl() is needed and arpa/inet.h provides this. Signed-off-by: Joe Stringer <joestringer@nicira.com> Acked-by: Jarno Rajahalme <jrajahalme@nicira.com>
* lib: Add API to set program name and versionThomas Graf2014-11-251-4/+2
| | | | | | | Required to have reasonable logging messages. Signed-off-by: Thomas Graf <tgraf@noironetworks.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: Introduce ovs_scan_len()Pravin B Shelar2014-11-121-0/+1
| | | | | | | | | | | | This is similar to ovs_scan but takes int pointer as extra parameter, this pointer point to starting index of the string. On successful scan this API stores number of characters scanned. This API is useful for parsing complex odp actions e.g. tun_push action. Signed-off-by: Pravin B Shelar <pshelar@nicira.com> Acked-by: Thomas Graf <tgraf@noironetworks.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: Add be32_prefix_mask().Jarno Rajahalme2014-11-111-0/+9
| | | | | | | | | | Shifting a 32-bit entity by 32 bits is undefined behavior. As we have 3 cases where we may hit this, it is a time to introduce a helper for this. VMware-BZ: #1355026 Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Pravin B Shelar <pshelar@nicira.com>
* ovs_assert, tests: Support NDEBUG.Jarno Rajahalme2014-10-301-5/+6
| | | | | | | | | | | | | | | | | ./configure accepts --enable-ndebug option. Make ovs_assert() honor it, and make sure all test programs disable it. The order of include files in test programs is also made uniform: 1. #include <config.h> 2. #undef NDEBUG 3. Include file of the test subject (to make sure it itself has sufficient include directives). 4. System includes in alphapetical order. 5. OVS includes in aplhapetical order. Suggested-by: Ben Pfaff <blp@nicira.com> Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: Make hexits_value() support 64-bit integers too.Ben Pfaff2014-10-081-1/+1
| | | | | Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
* util: New function bitwise_scan().Ben Pfaff2014-10-071-0/+2
| | | | | | | | | | This will acquire its first user in an upcoming commit. This implementation is not optimized at all but it doesn't matter for the purpose for which I intend to initially use it. Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
* lib/bitmap: Faster bitmap functions.Jarno Rajahalme2014-10-071-1/+1
| | | | | | | | | | | | | | | | | | | | | | Replace bitwise loops with a single operation, inline all bitmap functions. Inlining allows the compiler to remove unnecessary code due to some parameters being compile-time constants. Before: $ tests/ovstest test-bitmap benchmark 1000000 bitmap equal: 341 ms bitmap scan: 8089 ms After: $ tests/ovstest test-bitmap benchmark 1000000 bitmap equal: 152 ms bitmap scan: 146 ms Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Co-authored-by: Kmindg <kmindg@gmail.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: Use MSVC compiler intrinsic for clz and ctz.Gurucharan Shetty2014-10-061-0/+36
| | | | | | | | Using the compiler intrinsic shows approximately around 25% speed up with some classifier specific unit tests. Signed-off-by: Gurucharan Shetty <gshetty@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* compiler: Define NO_RETURN for MSVC.Gurucharan Shetty2014-09-151-10/+10
| | | | | | | | | | | | | To prevent warnings such as "Not all control paths return a value", we should define NO_RETURN for MSVC. Currently for gcc, we add NO_RETURN at the end of function declaration. But for MSVC, "__declspec(noreturn)" is needed at the beginning of function declaration. So this commit moves NO_RETURN to the beginning of the function declaration as it works with gcc and clang too. Signed-off-by: Gurucharan Shetty <gshetty@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* Avoid uninitialized variable warnings with OBJECT_OFFSETOF() in MSVC.Gurucharan Shetty2014-09-121-0/+7
| | | | | | | | | | | | | | Implementation of OBJECT_OFFSETOF() for non-GNUC compilers like MSVC causes "uninitialized variable" warnings. Since OBJECT_OFFSETOF() is indirectly used through all the *_FOR_EACH() (through ASSIGN_CONTAINER() and OBJECT_CONTAINING()) macros, the OVS build on Windows gets littered with "uninitialized variable" warnings. This patch attempts to workaround the problem. Signed-off-by: Gurucharan Shetty <gshetty@nicira.com> Acked-by: Alin Gabriel Serdean <aserdean@cloudbasesolutions.com> Acked-by: Saurabh Shah <ssaurabh@vmware.com> Acked-by: Ben Pfaff <blp@nicira.com>
* lib/util: Change is_all_zeros and is_all_ones to take a void *.Jarno Rajahalme2014-09-081-2/+2
| | | | | | | is_all_zeros() and is_all_ones() operate on bytes, but just like with memset, it is easier to use if the first argument is a void *. Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: fix compile warningsAnsis Atteka2014-07-081-1/+1
| | | | | | | | | | | | | | | This patch fixes two compile warnings introduced by commit 64b73291 ("util: create a copy of program_name"): 1. ../lib/util.c:457:5: error: passing argument 1 of 'free' discards 'const' qualifier from pointer target type; And 2. ../lib/util.c:463:5: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement] (affected only branch-2.3 that is C90 compliant and not the master) Reported-By: Joe Stringer <jstringer@nicira.com> Reported-By: Lorand Jakab <lojakab@cisco.com> Signed-Off-By: Ansis Atteka <aatteka@nicira.com> Acked-by: Joe Stringer <joestringer@nicira.com>
* util: Don't compile couple of unused function for Windows.Gurucharan Shetty2014-06-241-0/+2
| | | | | | | | | | | | basename() and dir_name() are not used for Windows and won't work well if used. So put a '#ifndef _WIN32' around them to prevent future calls. test-file_name.c tests the above 2 functions. It makes sense to merge this single function file with test-util.c and then not compile it for Windows. Signed-off-by: Gurucharan Shetty <gshetty@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: Move definition of HANDLE here.Ben Pfaff2014-06-131-0/+4
| | | | | | | | | | A few Open vSwitch source files use a type named HANDLE on Windows systems, in a way that makes it easier to avoid #ifdefs if we have a dummy definition on other platforms. <linux/types.h> was a really weird place for this dummy definition. This commit moves it to util.h. Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Gurucharan Shetty <gshetty@nicira.com>
* util: Remove duplicate CACHE_LINE_SIZE definition.Ben Pfaff2014-05-081-2/+0
| | | | | | Reported-by: Jarno Rajahalme <jrajahalme@nicira.com> Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Andy Zhou <azhou@nicira.com>
* lib: Add prefetch support (for GCC)Jarno Rajahalme2014-04-291-0/+14
| | | | | | | | Define OVS_PREFETCH() and OVS_PREFETCH_WRITE() using builtin prefetch for GCC, and ovs_prefetch_range() for prefetching a range of addresses. Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Ethan Jackson <ethan@nicira.com>
* lib/util: Input validation in str_to_uintZoltan Kiss2014-04-231-18/+1
| | | | | | | | | | | | | This function returns true when 's' is negative or greater than UINT_MAX. Also, the representation of 'int' and 'unsigned int' is implementation dependent, so converting [INT_MAX..UINT_MAX] values with str_to_int is fragile. Instead, we should convert straight to 'long long' and do a boundary check before returning the converted value. This patch also move the function to the .c file as it's not-trivial now, and deletes the other str_to_u* functions as they are not used. Signed-off-by: Zoltan Kiss <zoltan.kiss@citrix.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: xleep for Windows.Gurucharan Shetty2014-03-311-1/+1
| | | | | | | | | | | | | Windows does not have a sleep(seconds). But it does have a Sleep(milliseconds). Sleep() in windows does not have a return value. Since we are not using the return value for xsleep() anywhere as of now, don't return any. Introduced by commit 275eebb9 (utils: Introduce xsleep for RCU quiescent state) CC: Pravin B Shelar <pshelar@nicira.com> Signed-off-by: Gurucharan Shetty <gshetty@nicira.com> Acked-by: Pravin B Shelar <pshelar@nicira.com>
* lib/ofpbuf: CompactJarno Rajahalme2014-03-291-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch shrinks the struct ofpbuf from 104 to 48 bytes on 64-bit systems, or from 52 to 36 bytes on 32-bit systems (counting in the 'l7' removal from an earlier patch). This may help contribute to cache efficiency, and will speed up initializing, copying and manipulating ofpbufs. This is potentially important for the DPDK datapath, but the rest of the code base may also see a little benefit. Changes are: - Remove 'l7' pointer (previous patch). - Use offsets instead of layer pointers for l2_5, l3, and l4 using 'l2' as basis. Usually 'data' is the same as 'l2', but this is not always the case (e.g., when parsing or constructing a packet), so it can not be easily used as the offset basis. Also, packet parsing is faster if we do not need to maintain the offsets each time we pull data from the ofpbuf. - Use uint32_t for 'allocated' and 'size', as 2^32 is enough even for largest possible messages/packets. - Use packed enum for 'source'. - Rearrange to avoid unnecessary padding. - Remove 'private_p', which was used only in two cases, both of which had the invariant ('l2' == 'data'), so we can temporarily use 'l2' as a private pointer. Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* utils: Introduce xsleep for RCU quiescent statePravin2014-03-211-0/+1
| | | | | Signed-off-by: Pravin B Shelar <pshelar@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: New functions for allocating memory while avoiding false sharing.Ben Pfaff2014-03-181-1/+5
| | | | | | | This factors code out of fat-rwlock, making it easily usable by other code. Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Andy Zhou <azhou@nicira.com>
* util: Move CACHE_LINE_SIZE here.Ben Pfaff2014-03-131-0/+5
| | | | | | | It will come in handy elsewhere in upcoming commits. Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Andy Zhou <azhou@nicira.com>
* util: New macro PAD_SIZE.Ben Pfaff2014-03-131-0/+3
| | | | | | | | | | PAD_SIZE(x,y) is a little shorter and may have a more obvious meaning than ROUND_UP(x,y) - x. I intend to add more users in an upcoming comment. Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Andy Zhou <azhou@nicira.com>
* ovsdb-server: Truncate file for Windows.Gurucharan Shetty2014-03-121-0/+1
| | | | | | | | There is no ftruncate() in visual studio. There is a _chsize_s() which has a similar functionality. Signed-off-by: Gurucharan Shetty <gshetty@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: Pre-allocate buffer for ovs_lasterror_to_string().Gurucharan Shetty2014-02-141-0/+1
| | | | | | | | This lets us call ovs_lasterror_to_string() and not having to do an extra call of LocalFree() on the returned string. Signed-off-by: Gurucharan Shetty <gshetty@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: Make xreadlink a static function.Gurucharan Shetty2014-02-131-1/+0
| | | | | | | It is only used in util.c Signed-off-by: Gurucharan Shetty <gshetty@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: A generic function to convert error to string for windows.Gurucharan Shetty2014-01-281-0/+5
| | | | | | | More users will be added in an upcoming commit. Signed-off-by: Gurucharan Shetty <gshetty@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* Rename NOT_REACHED to OVS_NOT_REACHEDHarold Lim2013-12-171-1/+1
| | | | | | | | This allows other libraries to use util.h that has already defined NOT_REACHED. Signed-off-by: Harold Lim <haroldl@vmware.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* Update openvswitch to allow linking from C++ projectsHarold Lim2013-12-171-1/+1
| | | | | | | | The input variable of ovs_scan is changed from 'template' to 'format'. template is a keyword in C++. Signed-off-by: Harold Lim <haroldl@vmware.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* lib/util: Make some functions in util.c inlineHelmut Schaa2013-12-131-3/+18
| | | | | | | | | | str_to_uint, str_to_ulong and str_to_ullong are just wrappers around the corresponding signed functions. Move these to util.h and make them inline saving some library exports and letting the compiler do some more magic. Signed-off-by: Helmut Schaa <helmut.schaa@googlemail.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* lib/util: More portable use of builtin popcnt.Jarno Rajahalme2013-12-121-28/+34
| | | | | | | | | | | | | - Use the GCC predefined macro __POPCNT__ to detect the availability of fast __builtin_popcnt function. - Use portable preprocessor macros to detect 64-bit build. - Only define the 32-bit parts when needed and declare the count_1bits_8 at file scope to silence a warning. This time I have tested all code paths to make sure no warnigns are generated. Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Reviewed-by: Simon Horman <horms@verge.net.au>
* util: Fix sparse warning.Ben Pfaff2013-12-101-1/+2
| | | | | | | | | | | | Without this commit, sparse warns: lib/util.c:921:15: warning: symbol 'count_1bits_8' was not declared. Should it be static? Introduced by commit c3cc4d2dd2658 (util: Better count_1bits().). Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Jarno Rajahalme <jrajahalme@nicira.com>
* util: Better count_1bits().Jarno Rajahalme2013-12-061-1/+42
| | | | | | | | | | | | | | | | | | | | | | | Inline, use another well-known algorithm for 64-bit builds, and use builtins when they are known to be fast at compile time. A 32-bit version of the alternate algorithm is slower than the existing implementation, so the old one is used for 32-bit builds. Inline assembler would be a bit faster on 32-bit i7 build, but we use the GCC builtin for portability. It should be stressed builds for specific CPUs do not work on others CPUs, and that OVS build system or runtime does not currently support CPU detection. Speed improvement v.s. existing implementation / GCC 4.7 __builtin_popcountll(): i386: 64% (inlining) / 380% i386 on i7: 240% (inlining + builtin) / 820% x86_64: 59% (inlining + different algorithm) / 190% x86_64 on i7: 370% (inlining + builtin) / 0% Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* lib/util: Add clz32() and clz64().Jarno Rajahalme2013-12-031-4/+40
| | | | | | | | | Count leading zeroes using builtin if available. Make log_2_floor() use raw_clz() and inline log_2_floor() and log_2_ceil(). Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* lib/util: Rename ctz() as ctz32().Jarno Rajahalme2013-12-031-3/+3
| | | | | | | ctz() returns 32 for zero input, and we already have ctz64(), so it makes sense to rename ctz() as ctz32(). Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* Avoid printf type modifiers not supported by MSVC C runtime library.Alin Serdean2013-11-251-0/+17
| | | | | | | | | | | | | The MSVC C library printf() implementation does not support the 'z', 't', 'j', or 'hh' format specifiers. This commit changes the Open vSwitch code to avoid those format specifiers, switching to standard macros from <inttypes.h> where available and inventing new macros resembling them where necessary. It also updates CodingStyle to specify the macros' use and adds a Makefile rule to report violations. Signed-off-by: Alin Serdean <aserdean@cloudbasesolutions.com> Co-authored-by: Ben Pfaff <blp@nicira.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: Rename popcount to count_1bitsBen Pfaff2013-11-191-1/+1
| | | | | | | | | | | This avoids a conflict with NetBSD's strings.h/libc. (http://netbsd.gw.com/cgi-bin/man-cgi?popcount++NetBSD-current) The new name is suggested by Ben Pfaff. Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp> Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Jarno Rajahalme <jrajahalme@gmail.com>
* util: Make popcount() handle 64-bit integers, not separate popcount64().Ben Pfaff2013-11-181-16/+1
| | | | | | | | | | | | | | | Having a single function that can do popcount() on any integer type is easier for callers to get right. The implementation is probably slower if the caller actually provides a 32-bit (or shorter) integer, but the only existing callers always provide a full 64-bit integer so this seems unimportant for now. This also restores use, in practice, of the optimized implementation of population count. (As the comment on popcount32() says, this version is 2x faster than __builtin_popcount().) Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Jarno Rajahalme <jrajahalme@nicira.com>
* util: Make raw_ctz() accept 64-bit integers.Ben Pfaff2013-11-181-20/+11
| | | | | | | | | Having a single function that can do raw_ctz() on any integer type is easier for callers to get right, and there is no real downside in the implementation. Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Jarno Rajahalme <jrajahalme@nicira.com>
* lib/util: Add ctz64() and popcount64().Jarno Rajahalme2013-11-181-3/+35
| | | | | | | Add raw_ctz64(), ctz64(), and popcount64() using builtins when available. Signed-off By: Jarno Rajahalme <jrajahalme@nicira.com> Acked-by: Ben Pfaff <blp@nicira.com>
* util: New function ovs_scan().Ben Pfaff2013-11-151-0/+2
| | | | | | | | | | This new function is essentially an implementation of sscanf() with slightly different behavior (see the comment) that is more convenient for Open vSwitch internal use. Also, this implementation ought to work out of the box on Windows, which has a defective sscanf() that lacks the 'hh' modifier required to scan into a char variable. Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: Allow set_subprogram_name() to take a printf() format string.Ben Pfaff2013-11-021-1/+1
| | | | | | | | | | | | | This will be convenient in an upcoming commit. I had to add -Wno-format-zero-length to suppress a GCC warning about a zero-length format string in this monitor_daemon() call: set_subprogram_name(""); I don't know what that warning is good for anyway, and I guess the Clang developers don't either because Clang didn't warn. Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Ethan Jackson <ethan@nicira.com>
* util: use gcc builtins to better check array sizesFlavio Leitner2013-10-021-1/+16
| | | | | | | | | | | GCC provides two useful builtin functions that can help to improve array size checking during compilation. This patch contains no functional changes, but it makes it easier to detect mistakes. Signed-off-by: Flavio Leitner <fbl@redhat.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* clang: Fix the alignment warning.Alex Wang2013-07-231-0/+4
| | | | | | | | This commit fixes the warning issued by 'clang' when pointer is casted to one with greater alignment. Signed-off-by: Alex Wang <alexw@nicira.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* clang: Fix the "expression result unused" warning.Alex Wang2013-07-221-2/+2
| | | | | | | | | | This commit makes macro function "ASSIGN_CONTAINER()" evaluates to "(void)0". This is to avoid the 'clang' warning: "expression result unused", since most of time, the final evaluated value is not used. Signed-off-by: Alex Wang <alexw@nicira.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: New macros ROUND_UP_POW2, ROUND_DOWN_POW2.Ben Pfaff2013-07-171-0/+19
| | | | Signed-off-by: Ben Pfaff <blp@nicira.com>
* util: Make subprogram_name thread-specific.Ben Pfaff2013-07-121-1/+3
| | | | Signed-off-by: Ben Pfaff <blp@nicira.com>
* New function ovs_strerror() as a thread-safe replacement for strerror().Ben Pfaff2013-06-281-0/+1
| | | | Signed-off-by: Ben Pfaff <blp@nicira.com>