cvs: php4 /ext/ircg ircg.c

From: Date: Fri, 12 Jan 2001 13:35:30 +0000
Subject: cvs: php4 /ext/ircg ircg.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-3567@lists.php.net to get a copy of this message
sas Fri Jan 12 05:35:30 2001 EDT Modified files: /php4/ext/ircg ircg.c Log: Try to be more memory friendly by avoiding storing redundant data. Index: php4/ext/ircg/ircg.c diff -u php4/ext/ircg/ircg.c:1.14 php4/ext/ircg/ircg.c:1.15 --- php4/ext/ircg/ircg.c:1.14 Wed Jan 10 13:06:17 2001 +++ php4/ext/ircg/ircg.c Fri Jan 12 05:35:30 2001 @@ -29,8 +29,9 @@ /* This module is not intended for thread-safe use */ -static HashTable h_fd2irconn; -static HashTable h_irconn; +static HashTable h_fd2irconn; /* fd's to Integer IDs */ +static HashTable h_irconn; /* Integer IDs to php_irconn_t * */ +static HashTable h_fmt_msgs; /* Integer IDs to php_fmt_msgs_t * */ static int irconn_id = 1; /* Format string numbers */ @@ -63,12 +64,15 @@ #include "if_irc.h" typedef struct { + char *fmt_msgs[NO_FMTS]; +} php_fmt_msgs_t; + +typedef struct { irconn_t conn; - const char *username; zend_llist buffer; int fd; int irconn_id; - char *fmt_msgs[NO_FMTS]; + php_fmt_msgs_t *fmt_msgs; } php_irconn_t; static char *fmt_msgs_default[] = { @@ -84,9 +88,9 @@ }; #define MSG(conn, type) \ - conn->fmt_msgs[type]?conn->fmt_msgs[type]:fmt_msgs_default[type] + (conn->fmt_msgs&&conn->fmt_msgs->fmt_msgs[type])?conn->fmt_msgs->fmt_msgs[type]:fmt_msgs_default[type] -php_irconn_t *lookup_irconn(int id) +static php_irconn_t *lookup_irconn(int id) { php_irconn_t **ret; @@ -95,6 +99,15 @@ return *ret; } +static php_fmt_msgs_t *lookup_fmt_msgs(zval **id) +{ + php_fmt_msgs_t *ret; + + if (zend_hash_find(&h_fmt_msgs, Z_STRVAL_PP(id), Z_STRLEN_PP(id), (void **) &ret) == FAILURE) + return NULL; + return ret; +} + static void irconn_dtor(void *dummy) { php_irconn_t **conn = dummy; @@ -102,6 +115,16 @@ irc_disconnect(&(*conn)->conn, "Browser connection closed"); } +static void fmt_msgs_dtor(void *dummy) +{ + php_fmt_msgs_t *fmt_msgs = dummy; + int i; + + for (i = 0; i < NO_FMTS; i++) { + if (fmt_msgs->fmt_msgs[i]) free(fmt_msgs->fmt_msgs[i]); + } +} + static void buffer_dtor(void *dummy) { smart_str *msg = dummy; @@ -118,11 +141,6 @@ zend_hash_index_del(&h_fd2irconn, conn->fd); conn->fd = -2; zend_hash_index_del(&h_irconn, conn->irconn_id); - - for (i = 0; i < NO_FMTS; i++) { - if (conn->fmt_msgs[i]) free(conn->fmt_msgs[i]); - } - free((char *) conn->username); zend_llist_destroy(&conn->buffer); free(conn); @@ -219,7 +237,7 @@ const char *p; char c; int mode = 0; - smart_str new_msg = {0}; + static smart_str new_msg; if (fmt[0] == '\0') { result->len = 0; @@ -227,6 +245,7 @@ } if (msg) { + new_msg.len = 0; mirc_color(msg, &new_msg); } @@ -267,10 +286,6 @@ } } - if (msg) { - smart_str_free_ex(&new_msg, 1); - } - smart_str_0(result); } @@ -330,10 +345,10 @@ smart_str m = {0}; if (chan) { - format_msg(MSG(conn, FMT_MSG_CHAN), chan, conn->username, from, msg->c, + format_msg(MSG(conn, FMT_MSG_CHAN), chan, ircc->username, from, msg->c, &m); } else { - format_msg(MSG(conn, FMT_MSG_PRIV_TO_ME), NULL, conn->username, from, + format_msg(MSG(conn, FMT_MSG_PRIV_TO_ME), NULL, ircc->username, from, msg->c, &m); } @@ -347,7 +362,7 @@ smart_str m = {0}; format_msg(MSG(conn, fatal ? FMT_MSG_FATAL_ERROR : FMT_MSG_ERROR), NULL, - conn->username, "IRC SERVER", msg->c, &m); + ircc->username, "IRC SERVER", msg->c, &m); msg_send(conn, &m); smart_str_free_ex(&m, 1); @@ -502,12 +517,62 @@ RETURN_FALSE; } +PHP_FUNCTION(ircg_lookup_format_messages) +{ + zval **p1; + + if (ZEND_NUM_ARGS() != 1 + || zend_get_parameters_ex(1, &p1) == FAILURE) + WRONG_PARAM_COUNT; + + convert_to_string_ex(p1); + + if (lookup_fmt_msgs(p1)) + RETURN_TRUE; + RETVAL_FALSE; +} + +PHP_FUNCTION(ircg_register_format_messages) +{ + zval **p1, **p2; + HashTable *h; + int i; + php_fmt_msgs_t fmt_msgs; + zval **arg; + + if (ZEND_NUM_ARGS() != 2 + || zend_get_parameters_ex(2, &p1, &p2) == FAILURE) + WRONG_PARAM_COUNT; + + convert_to_string_ex(p1); + + if (Z_TYPE_PP(p2) != IS_ARRAY) { + php_error(E_WARNING, "The first parameter should be an array"); + RETURN_FALSE; + } + + h = HASH_OF(*p2); + + for (i = 0; i < NO_FMTS; i++) { + if (zend_hash_index_find(h, i, (void **) &arg) == SUCCESS) { + convert_to_string_ex(arg); + fmt_msgs.fmt_msgs[i] = strdup(Z_STRVAL_PP(arg)); + } else + fmt_msgs.fmt_msgs[i] = NULL; + } + zend_hash_update(&h_fmt_msgs, Z_STRVAL_PP(p1), Z_STRLEN_PP(p1), + &fmt_msgs, sizeof(fmt_msgs), NULL); + + RETVAL_TRUE; +} + PHP_FUNCTION(ircg_pconnect) { zval **p1, **p2, **p3, **p4 = NULL; const char *username; const char *server = "0"; int port = 6667; + php_fmt_msgs_t *fmt_msgs = NULL; php_irconn_t *conn; if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 4 @@ -516,10 +581,8 @@ switch (ZEND_NUM_ARGS()) { case 4: - if (Z_TYPE_PP(p4) != IS_ARRAY) { - php_error(E_WARNING, "Fourth parameter to ircg_pconnect() shall be an array"); - RETURN_FALSE; - } + convert_to_string_ex(p4); + fmt_msgs = lookup_fmt_msgs(p4); case 3: convert_to_long_ex(p3); port = Z_LVAL_PP(p3); @@ -536,31 +599,14 @@ * so we have to allocate it ourselves. */ conn = malloc(sizeof(*conn)); - conn->username = strdup(username); conn->fd = -1; if (irc_connect(username, NULL, msg_handler, quit_handler, error_handler, conn, server, port, &conn->conn)) { - free((char *) conn->username); free(conn); RETURN_FALSE; - } - if (p4) { - HashTable *h; - int i; - zval **arg; - - h = HASH_OF(*p4); - for (i = 0; i < NO_FMTS; i++) { - if (zend_hash_index_find(h, i, (void **) &arg) == SUCCESS) { - convert_to_string_ex(arg); - conn->fmt_msgs[i] = strdup(Z_STRVAL_PP(arg)); - } else - conn->fmt_msgs[i] = NULL; - } - } else { - memset(conn->fmt_msgs, 0, sizeof(conn->fmt_msgs)); } + conn->fmt_msgs = fmt_msgs; irconn_id++; conn->irconn_id = irconn_id; zend_hash_index_update(&h_irconn, irconn_id, &conn, sizeof(conn), NULL); @@ -611,11 +657,11 @@ case '#': case '&': format_msg(MSG(conn, FMT_MSG_CHAN), Z_STRVAL_PP(recipient), - NULL, conn->username, l.c, &m); + NULL, conn->conn.username, l.c, &m); break; default: format_msg(MSG(conn, FMT_MSG_PRIV_FROM_ME), NULL, - Z_STRVAL_PP(recipient), conn->username, l.c, &m); + Z_STRVAL_PP(recipient), conn->conn.username, l.c, &m); } msg_send(conn, &m); @@ -645,6 +691,7 @@ REGISTER_INI_ENTRIES(); */ + zend_hash_init(&h_fmt_msgs, 0, NULL, fmt_msgs_dtor, 1); zend_hash_init(&h_fd2irconn, 0, NULL, NULL, 1); zend_hash_init(&h_irconn, 0, NULL, irconn_dtor, 1); return SUCCESS; @@ -655,6 +702,7 @@ /* Remove comments if you have entries in php.ini UNREGISTER_INI_ENTRIES(); */ + zend_hash_destroy(&h_fmt_msgs); zend_hash_destroy(&h_irconn); zend_hash_destroy(&h_fd2irconn); return SUCCESS;

« previous php.cvs (#3567) next »