Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use the restrict keyword in some places.

  • Loading branch information...
commit 7f3dc65d0f1fb05f4a80991986b71548cf662786 1 parent d3c0703
Hongli Lai FooBarWidget authored
9 ext/common/Utils/IOUtils.cpp
View
@@ -744,7 +744,8 @@ writeExact(int fd, const void *data, unsigned int size, unsigned long long *time
void
writeExact(int fd, const StaticString &data, unsigned long long *timeout) {
- writeExact(fd, data.c_str(), data.size(), timeout);
+ const char * restrict data_ptr = data.data();
+ writeExact(fd, data_ptr, data.size(), timeout);
}
/**
@@ -792,7 +793,7 @@ staticStringArrayToIoVec(const StaticString ary[], size_t count, struct iovec *v
*/
static void
findDataPositionIndexAndOffset(struct iovec data[], size_t count,
- size_t position, size_t *index, size_t *offset)
+ size_t position, size_t * restrict index, size_t * restrict offset)
{
size_t i;
size_t begin = 0;
@@ -817,7 +818,7 @@ findDataPositionIndexAndOffset(struct iovec data[], size_t count,
}
ssize_t
-gatheredWrite(int fd, const StaticString data[], unsigned int dataCount, string &restBuffer) {
+gatheredWrite(int fd, const StaticString *data, unsigned int dataCount, string &restBuffer) {
size_t totalSize, iovCount, i;
ssize_t ret;
@@ -961,7 +962,7 @@ eraseBeginningOfIoVec(struct iovec *iov, size_t count, size_t index, size_t offs
}
void
-gatheredWrite(int fd, const StaticString data[], unsigned int count, unsigned long long *timeout) {
+gatheredWrite(int fd, const StaticString *data, unsigned int count, unsigned long long *timeout) {
struct iovec iov[count];
size_t total, iovCount;
size_t written = 0;
46 ext/common/Utils/IOUtils.h
View
@@ -34,6 +34,7 @@
#include <netdb.h>
#include <string>
#include <vector>
+#include <oxt/macros.hpp>
#include <StaticString.h>
#include <FileDescriptor.h>
@@ -81,7 +82,9 @@ string parseUnixSocketAddress(const StaticString &address);
*
* @throw ArgumentException <tt>address</tt> is not a valid TCP socket address.
*/
-void parseTcpSocketAddress(const StaticString &address, string &host, unsigned short &port);
+void parseTcpSocketAddress(const StaticString & restrict_ref address,
+ string & restrict_ref host,
+ unsigned short & restrict_ref port);
/**
* Returns whether the given socket address (as accepted by getSocketAddressType())
@@ -103,7 +106,10 @@ void setNonBlocking(int fd);
* Try to call the Linux accept4() system call. If the system call is
* not available, then -1 is returned and errno is set to ENOSYS.
*/
-int callAccept4(int sock, struct sockaddr *addr, socklen_t *addr_len, int options);
+int callAccept4(int sock,
+ struct sockaddr * restrict addr,
+ socklen_t * restrict addr_len,
+ int options);
/**
* Resolves the given host name and returns a list of IP addresses.
@@ -116,7 +122,9 @@ int callAccept4(int sock, struct sockaddr *addr, socklen_t *addr_len, int option
* IP addresses, then these addresses will be shuffled before they are
* returned in order to improve load balancing.
*/
-vector<string> resolveHostname(const string &hostname, unsigned int port = 0, bool shuffle = true);
+vector<string> resolveHostname(const string &hostname,
+ unsigned int port = 0,
+ bool shuffle = true);
/**
* Create a new Unix or TCP server socket, depending on the address type.
@@ -134,7 +142,9 @@ vector<string> resolveHostname(const string &hostname, unsigned int port = 0, bo
* @throws boost::thread_interrupted A system call has been interrupted.
* @ingroup Support
*/
-int createServer(const StaticString &address, unsigned int backlogSize = 0, bool autoDelete = true);
+int createServer(const StaticString &address,
+ unsigned int backlogSize = 0,
+ bool autoDelete = true);
/**
* Create a new Unix server socket which is bounded to <tt>filename</tt>.
@@ -149,7 +159,9 @@ int createServer(const StaticString &address, unsigned int backlogSize = 0, bool
* @throws boost::thread_interrupted A system call has been interrupted.
* @ingroup Support
*/
-int createUnixServer(const StaticString &filename, unsigned int backlogSize = 0, bool autoDelete = true);
+int createUnixServer(const StaticString &filename,
+ unsigned int backlogSize = 0,
+ bool autoDelete = true);
/**
* Create a new TCP server socket which is bounded to the given address and port.
@@ -166,7 +178,9 @@ int createUnixServer(const StaticString &filename, unsigned int backlogSize = 0,
* @throws boost::thread_interrupted A system call has been interrupted.
* @ingroup Support
*/
-int createTcpServer(const char *address = "0.0.0.0", unsigned short port = 0, unsigned int backlogSize = 0);
+int createTcpServer(const char *address = "0.0.0.0",
+ unsigned short port = 0,
+ unsigned int backlogSize = 0);
/**
* Connect to a server at the given address in a blocking manner.
@@ -223,7 +237,8 @@ struct NUnix_State {
* @throws boost::thread_interrupted A system call has been interrupted.
* @ingroup Support
*/
-void setupNonBlockingUnixSocket(NUnix_State &state, const StaticString &filename);
+void setupNonBlockingUnixSocket(NUnix_State & restrict_ref state,
+ const StaticString & restrict_ref filename);
/**
* Connect a Unix domain socket in non-blocking mode.
@@ -270,7 +285,9 @@ struct NTCP_State {
* @throws boost::thread_interrupted A system call has been interrupted.
* @ingroup Support
*/
-void setupNonBlockingTcpSocket(NTCP_State &state, const StaticString &hostname, int port);
+void setupNonBlockingTcpSocket(NTCP_State & restrict_ref state,
+ const StaticString & restrict_ref hostname,
+ int port);
/**
* Connect a TCP socket in non-blocking mode.
@@ -302,7 +319,8 @@ struct NConnect_State {
* @throws boost::thread_interrupted A system call has been interrupted.
* @ingroup Support
*/
-void setupNonBlockingSocket(NConnect_State &state, const StaticString &address);
+void setupNonBlockingSocket(NConnect_State & restrict_ref state,
+ const StaticString & restrict_ref address);
/**
* Connect a socket in non-blocking mode.
@@ -387,7 +405,7 @@ bool waitUntilWritable(int fd, unsigned long long *timeout);
* <tt>timeout</tt> microseconds.
* @throws boost::thread_interrupted
*/
-unsigned int readExact(int fd, void *buf, unsigned int size, unsigned long long *timeout = NULL);
+unsigned int readExact(int fd, void * restrict buf, unsigned int size, unsigned long long * restrict timeout = NULL);
/**
* Writes a block of data to the given file descriptor and blocks until everything
@@ -415,8 +433,8 @@ unsigned int readExact(int fd, void *buf, unsigned int size, unsigned long long
* <tt>timeout</tt> microseconds.
* @throws boost::thread_interrupted
*/
-void writeExact(int fd, const void *data, unsigned int size, unsigned long long *timeout = NULL);
-void writeExact(int fd, const StaticString &data, unsigned long long *timeout = NULL);
+void writeExact(int fd, const void * restrict data, unsigned int size, unsigned long long * restrict timeout = NULL);
+void writeExact(int fd, const StaticString & restrict_ref data, unsigned long long * restrict timeout = NULL);
/**
* Writes a bunch of data to the given file descriptor using a gathering I/O interface.
@@ -446,7 +464,7 @@ void writeExact(int fd, const StaticString &data, unsigned long long *timeout =
* isn't related to non-blocking writes.
* @throws boost::thread_interrupted
*/
-ssize_t gatheredWrite(int fd, const StaticString data[], unsigned int dataCount, string &restBuffer);
+ssize_t gatheredWrite(int fd, const StaticString * restrict data, unsigned int dataCount, string & restrict_ref restBuffer);
/**
* Writes a bunch of data to the given file descriptor using a gathering I/O interface.
@@ -474,7 +492,7 @@ ssize_t gatheredWrite(int fd, const StaticString data[], unsigned int dataCount,
* <tt>timeout</tt> microseconds.
* @throws boost::thread_interrupted
*/
-void gatheredWrite(int fd, const StaticString data[], unsigned int dataCount, unsigned long long *timeout = NULL);
+void gatheredWrite(int fd, const StaticString * restrict data, unsigned int dataCount, unsigned long long * restrict timeout = NULL);
/**
* Sets a writev-emulating function that gatheredWrite() should call instead of the real writev().
19 ext/common/Utils/StrIntUtils.h
View
@@ -30,6 +30,7 @@
#include <sstream>
#include <cstddef>
#include <ctime>
+#include <oxt/macros.hpp>
#include <StaticString.h>
namespace Passenger {
@@ -74,8 +75,12 @@ bool startsWith(const StaticString &str, const StaticString &substr);
* @param sep The separator to use.
* @param output The vector to write the output to.
*/
-void split(const StaticString &str, char sep, vector<string> &output);
-void split(const StaticString &str, char sep, vector<StaticString> &output);
+void split(const StaticString & restrict_ref str,
+ char sep,
+ vector<string> & restrict_ref output);
+void split(const StaticString & restrict_ref str,
+ char sep,
+ vector<StaticString> & restrict_ref output);
/**
* Split the given string using the given separator. Includes the
@@ -85,8 +90,12 @@ void split(const StaticString &str, char sep, vector<StaticString> &output);
* @param sep The separator to use.
* @param output The vector to write the output to.
*/
-void splitIncludeSep(const StaticString &str, char sep, vector<string> &output);
-void splitIncludeSep(const StaticString &str, char sep, vector<StaticString> &output);
+void splitIncludeSep(const StaticString & restrict_ref str,
+ char sep,
+ vector<string> & restrict_ref output);
+void splitIncludeSep(const StaticString & restrict_ref str,
+ char sep,
+ vector<StaticString> & restrict_ref output);
/**
* Look for 'toFind' inside 'str', replace it with 'replaceWith' and return the result.
@@ -146,7 +155,7 @@ string toHex(const StaticString &data);
* Convert the given binary data to hexadecimal. This form accepts an
* output buffer which must be at least <tt>data.size() * 2</tt> bytes large.
*/
-void toHex(const StaticString &data, char *output, bool upperCase = false);
+void toHex(const StaticString & restrict_ref data, char * restrict output, bool upperCase = false);
/**
* Convert the given integer to some other radix, placing
38 ext/oxt/macros.hpp
View
@@ -2,7 +2,7 @@
* OXT - OS eXtensions for boosT
* Provides important functionality necessary for writing robust server software.
*
- * Copyright (c) 2010, 2011, 2012 Phusion
+ * Copyright (c) 2010-2013 Phusion
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -36,27 +36,49 @@
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
-#if (defined(__GNUC__) && (__GNUC__ > 2) && !defined(OXT_DEBUG)) || defined(IN_DOXYGEN)
+#if (defined(__GNUC__) && (__GNUC__ > 2)) || defined(IN_DOXYGEN)
/**
* Indicate that the given expression is likely to be true.
* This allows the CPU to better perform branch prediction.
- *
- * Defining OXT_DEBUG will cause this macro to become an
- * empty stub.
*/
#define OXT_LIKELY(expr) __builtin_expect((expr), 1)
/**
* Indicate that the given expression is likely to be false.
* This allows the CPU to better perform branch prediction.
- *
- * Defining OXT_DEBUG will cause this macro to become an
- * empty stub.
*/
#define OXT_UNLIKELY(expr) __builtin_expect((expr), 0)
+
+ /**
+ * Force inlining of the given function.
+ */
+ #define OXT_FORCE_INLINE __attribute__((always_inline))
+
+ #if __GNUC__ >= 4
+ #define OXT_RESTRICT __restrict__
+ #else
+ #define OXT_RESTRICT
+ #endif
+ #ifndef restrict
+ /**
+ * The C99 'restrict' keyword, now usable in C++.
+ */
+ #define restrict OXT_RESTRICT
+ #endif
+ #ifndef restrict_ref
+ /**
+ * The C99 'restrict' keyword, for use with C++ references.
+ * On compilers that support 'restrict' in C++ but not on
+ * references, this macro does nothing.
+ */
+ #define restrict_ref OXT_RESTRICT
+ #endif
#else
#define OXT_LIKELY(expr) expr
#define OXT_UNLIKELY(expr) expr
+ #define OXT_FORCE_INLINE
+ #define restrict
+ #define restrict_ref
#endif
/*
Please sign in to comment.
Something went wrong with that request. Please try again.