cvs: Zend / zend_alloc.c zend_alloc.h
| From: | Sascha Schumann | Date: | Sat, 27 Apr 2002 11:49:06 +0000 |
| Subject: | cvs: Zend / zend_alloc.c zend_alloc.h | ||
| Groups: | php.zend-engine.cvs | ||
| Request: | Send a blank email to php-zend-engine-cvs+get-73@lists.php.net to get a copy of this message | ||
sas Sat Apr 27 07:49:06 2002 EDT
Modified files:
/Zend zend_alloc.c zend_alloc.h
Log:
If the size-operands of memset are constants, the compiler can
turn them into fast inline code.
So, instead of using ecalloc, we use emalloc + memset in macro form now.
emalloc will not return NULL, so the chosen macro form is safe.
This is not true for malloc(3). An inline function accomodates
our needs here.
Suggested by: http://www.mail-archive.com/dev%40httpd.apache.org/msg02492.html
Index: Zend/zend_alloc.c
diff -u Zend/zend_alloc.c:1.96 Zend/zend_alloc.c:1.97
--- Zend/zend_alloc.c:1.96 Tue Feb 26 13:59:25 2002
+++ Zend/zend_alloc.c Sat Apr 27 07:49:05 2002
@@ -248,22 +248,6 @@
}
-ZEND_API void *_ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
-{
- void *p;
- int final_size = size*nmemb;
-
- HANDLE_BLOCK_INTERRUPTIONS();
- p = _emalloc(final_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
- if (!p) {
- HANDLE_UNBLOCK_INTERRUPTIONS();
- return (void *) p;
- }
- memset(p, 0, final_size);
- HANDLE_UNBLOCK_INTERRUPTIONS();
- return p;
-}
-
ZEND_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC
ZEND_FILE_LINE_ORIG_DC)
{
Index: Zend/zend_alloc.h
diff -u Zend/zend_alloc.h:1.34 Zend/zend_alloc.h:1.35
--- Zend/zend_alloc.h:1.34 Tue Feb 26 13:59:25 2002
+++ Zend/zend_alloc.h Sat Apr 27 07:49:05 2002
@@ -75,7 +75,6 @@
ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
-ZEND_API void *_ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
ZEND_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC
ZEND_FILE_LINE_ORIG_DC);
ZEND_API char *_estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
ZEND_API char *_estrndup(const char *s, unsigned int length ZEND_FILE_LINE_DC
ZEND_FILE_LINE_ORIG_DC);
@@ -84,7 +83,7 @@
/* Standard wrapper macros */
#define emalloc(size) _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
#define efree(ptr) _efree((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
-#define ecalloc(nmemb, size) _ecalloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
+#define ecalloc(nmemb, size) memset(_emalloc((nmemb)*(size) ZEND_FILE_LINE_CC
ZEND_FILE_LINE_EMPTY_CC), 0, (nmemb)*(size))
#define erealloc(ptr, size) _erealloc((ptr), (size), 0 ZEND_FILE_LINE_CC
ZEND_FILE_LINE_EMPTY_CC)
#define erealloc_recoverable(ptr, size) _erealloc((ptr), (size), 1 ZEND_FILE_LINE_CC
ZEND_FILE_LINE_EMPTY_CC)
#define estrdup(s) _estrdup((s) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
@@ -94,17 +93,28 @@
/* Relay wrapper macros */
#define emalloc_rel(size) _emalloc((size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
#define efree_rel(ptr) _efree((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
-#define ecalloc_rel(nmemb, size) _ecalloc((nmemb), (size) ZEND_FILE_LINE_RELAY_CC
ZEND_FILE_LINE_CC)
+#define ecalloc_rel(nmemb, size) memset(_emalloc((nmemb)*(size) ZEND_FILE_LINE_RELAY_CC
ZEND_FILE_LINE_CC), 0, (nmemb)*(size))
#define erealloc_rel(ptr, size) _erealloc((ptr), (size), 0 ZEND_FILE_LINE_RELAY_CC
ZEND_FILE_LINE_CC)
#define erealloc_recoverable_rel(ptr, size) _erealloc((ptr), (size), 1 ZEND_FILE_LINE_RELAY_CC
ZEND_FILE_LINE_CC)
#define estrdup_rel(s) _estrdup((s) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
#define estrndup_rel(s, length) _estrndup((s), (length) ZEND_FILE_LINE_RELAY_CC
ZEND_FILE_LINE_CC)
#define persist_alloc_rel(p) _persist_alloc((p) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
+ZEND_API static inline void *zend_fast_calloc(size_t nmemb, size_t size)
+{
+ void *p;
+ size_t t = nmemb * size;
+
+ p = malloc(t);
+ if (p)
+ memset(p, 0, t);
+ return p;
+}
+
/* Selective persistent/non persistent allocation macros */
#define pemalloc(size, persistent) ((persistent)?malloc(size):emalloc(size))
#define pefree(ptr, persistent) ((persistent)?free(ptr):efree(ptr))
-#define pecalloc(nmemb, size, persistent) ((persistent)?calloc((nmemb), (size)):ecalloc((nmemb),
(size)))
+#define pecalloc(nmemb, size, persistent) ((persistent)?zend_fast_calloc((nmemb),
(size)):ecalloc((nmemb), (size)))
#define perealloc(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc((ptr),
(size)))
#define perealloc_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr),
(size)):erealloc_recoverable((ptr), (size)))
#define pestrdup(s, persistent) ((persistent)?strdup(s):estrdup(s))