Revision: 381
http://assorted.svn.sourceforge.net/assorted/?rev=381&view=rev
Author: yangzhang
Date: 2008-02-11 15:23:44 -0800 (Mon, 11 Feb 2008)
Log Message:
-----------
added scan
Modified Paths:
--------------
cpp-commons/trunk/src/commons/strings.h
Modified: cpp-commons/trunk/src/commons/strings.h
===================================================================
--- cpp-commons/trunk/src/commons/strings.h 2008-02-11 23:23:00 UTC (rev 380)
+++ cpp-commons/trunk/src/commons/strings.h 2008-02-11 23:23:44 UTC (rev 381)
@@ -6,6 +6,7 @@
#include <strings.h>
#include <commons/check.h>
+#include <commons/pointers.h>
namespace commons
{
@@ -77,6 +78,60 @@
return unsafe_strstr((char*) p, q, lim);
}
+ /**
+ * "Touch" the entire memory region. Useful for ensuring that some piece of
+ * memory is "warmed up." Visits each character. Note that this might be
+ * optimized out if the caller doesn't meaningfully use the (mostly
+ * meaningless) return value.
+ */
+ inline char
+ scan(const void* buf, size_t len)
+ {
+ char sum = 0;
+ const char* start = (const char*) buf;
+ for (const char* p = start; p < start + len; p++) {
+ sum += *p;
+ }
+ return sum;
+ }
+
+ /**
+ * "Touch" the entire memory region. Useful for ensuring that some piece of
+ * memory is "warmed up." Uses memcpy on 1024-byte chunks. Note that this
+ * might be optimized out if the caller doesn't meaningfully use the (mostly
+ * meaningless) return value.
+ */
+ inline char
+ scan_big_step(const void* buf, size_t len)
+ {
+ const size_t sz = 1024;
+ char tmp[sz];
+ const char* p = (const char*) buf;
+ const char* end = p + len;
+ for (; p + sz < end; p += 1024) {
+ memcpy(tmp, p, sz);
+ }
+ memcpy(tmp, p, end - p);
+ return sz == 0 ? 0 : tmp[0];
+ }
+
+// /**
+// * Find an int in the buffer. Return pointer to the first occurrence on
+// * success, NULL on failure.
+// */
+// inline const void*
+// findint(const void* buf, size_t len, int x)
+// {
+// const void* pend = (buf + len);
+// // Word-align the start and end pointers.
+// const int* end = (const int*) round_up(p, sizeof(int));
+// const int* start = (const int*) round_down(buf, sizeof(int));
+// for (const int* p = buf; p < end; ++p) {
+// if (*p == x) return p;
+// }
+// return NULL;
+// }
+
}
#endif
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|