Bug 464995 - avoid reads of size 1 in jemalloc. r=jasone,sr=stuart
--- a/memory/jemalloc/jemalloc.c
+++ b/memory/jemalloc/jemalloc.c
@@ -169,17 +169,17 @@
/*
* MALLOC_PAGEFILE causes all mmap()ed memory to be backed by temporary
* files, so that if a chunk is mapped, it is guaranteed to be swappable.
* This avoids asynchronous OOM failures that are due to VM over-commit.
*
* XXX OS X over-commits, so we should probably use mmap() instead of
* vm_allocate(), so that MALLOC_PAGEFILE works.
*/
-# define MALLOC_PAGEFILE
+//# define MALLOC_PAGEFILE
#endif
#ifdef MALLOC_PAGEFILE
/* Write size when initializing a page file. */
# define MALLOC_PAGEFILE_WRITE_SIZE 512
#endif
#ifdef MOZ_MEMORY_LINUX
@@ -5094,18 +5094,19 @@ malloc_ncpus(void)
#elif (defined(MOZ_MEMORY_LINUX))
#include <fcntl.h>
static inline unsigned
malloc_ncpus(void)
{
unsigned ret;
int fd, nread, column;
- char buf[1];
+ char buf[1024];
static const char matchstr[] = "processor\t:";
+ int i;
/*
* sysconf(3) would be the preferred method for determining the number
* of CPUs, but it uses malloc internally, which causes untennable
* recursion during malloc initialization.
*/
fd = open("/proc/cpuinfo", O_RDONLY);
if (fd == -1)
@@ -5115,30 +5116,33 @@ malloc_ncpus(void)
* lines. This treats hyperthreaded CPUs as multiple processors.
*/
column = 0;
ret = 0;
while (true) {
nread = read(fd, &buf, sizeof(buf));
if (nread <= 0)
break; /* EOF or error. */
-
- if (buf[0] == '\n')
- column = 0;
- else if (column != -1) {
- if (buf[0] == matchstr[column]) {
- column++;
- if (column == sizeof(matchstr) - 1) {
+ for (i = 0;i < nread;i++) {
+ char c = buf[i];
+ if (c == '\n')
+ column = 0;
+ else if (column != -1) {
+ if (c == matchstr[column]) {
+ column++;
+ if (column == sizeof(matchstr) - 1) {
+ column = -1;
+ ret++;
+ }
+ } else
column = -1;
- ret++;
- }
- } else
- column = -1;
+ }
}
}
+
if (ret == 0)
ret = 1; /* Something went wrong in the parser. */
close(fd);
return (ret);
}
#elif (defined(MOZ_MEMORY_DARWIN))
#include <mach/mach_init.h>