diff src/misc2.c @ 16764:ef00b6bc186b v8.1.1384

patch 8.1.1384: using "int" for alloc() often results in compiler warnings commit https://github.com/vim/vim/commit/964b3746b9c81e65887e2ac9a335f181db2bb592 Author: Bram Moolenaar <Bram@vim.org> Date: Fri May 24 18:54:09 2019 +0200 patch 8.1.1384: using "int" for alloc() often results in compiler warnings Problem: Using "int" for alloc() often results in compiler warnings. Solution: Use "size_t" and remove type casts. Remove alloc_check(), Vim only works with 32 bit ints anyway.
author Bram Moolenaar <Bram@vim.org>
date Fri, 24 May 2019 19:00:07 +0200
parents 77bcb5055fec
children fc58fee685e2
line wrap: on
line diff
--- a/src/misc2.c
+++ b/src/misc2.c
@@ -711,7 +711,7 @@ mem_pre_alloc_s(size_t *sizep)
 }
 
     static void
-mem_pre_alloc_l(long_u *sizep)
+mem_pre_alloc_l(size_t *sizep)
 {
     *sizep += sizeof(size_t);
 }
@@ -796,7 +796,7 @@ vim_mem_profile_dump(void)
 
 #ifdef FEAT_EVAL
     int
-alloc_does_fail(long_u size)
+alloc_does_fail(size_t size)
 {
     if (alloc_fail_countdown == 0)
     {
@@ -818,39 +818,39 @@ alloc_does_fail(long_u size)
 #define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
 
 /*
- * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
- * Use lalloc for larger blocks.
+ * The normal way to allocate memory.  This handles an out-of-memory situation
+ * as well as possible, still returns NULL when we're completely out.
  */
     char_u *
-alloc(unsigned size)
+alloc(size_t size)
 {
-    return (lalloc((long_u)size, TRUE));
+    return lalloc(size, TRUE);
 }
 
 /*
  * alloc() with an ID for alloc_fail().
  */
     char_u *
-alloc_id(unsigned size, alloc_id_T id UNUSED)
+alloc_id(size_t size, alloc_id_T id UNUSED)
 {
 #ifdef FEAT_EVAL
-    if (alloc_fail_id == id && alloc_does_fail((long_u)size))
+    if (alloc_fail_id == id && alloc_does_fail(size))
 	return NULL;
 #endif
-    return (lalloc((long_u)size, TRUE));
+    return lalloc(size, TRUE);
 }
 
 /*
  * Allocate memory and set all bytes to zero.
  */
     char_u *
-alloc_clear(unsigned size)
+alloc_clear(size_t size)
 {
     char_u *p;
 
-    p = lalloc((long_u)size, TRUE);
+    p = lalloc(size, TRUE);
     if (p != NULL)
-	(void)vim_memset(p, 0, (size_t)size);
+	(void)vim_memset(p, 0, size);
     return p;
 }
 
@@ -858,44 +858,26 @@ alloc_clear(unsigned size)
  * Same as alloc_clear() but with allocation id for testing
  */
     char_u *
-alloc_clear_id(unsigned size, alloc_id_T id UNUSED)
+alloc_clear_id(size_t size, alloc_id_T id UNUSED)
 {
 #ifdef FEAT_EVAL
-    if (alloc_fail_id == id && alloc_does_fail((long_u)size))
+    if (alloc_fail_id == id && alloc_does_fail(size))
 	return NULL;
 #endif
     return alloc_clear(size);
 }
 
 /*
- * alloc() with check for maximum line length
- */
-    char_u *
-alloc_check(unsigned size)
-{
-#if !defined(UNIX)
-    if (sizeof(int) == 2 && size > 0x7fff)
-    {
-	/* Don't hide this message */
-	emsg_silent = 0;
-	emsg(_("E340: Line is becoming too long"));
-	return NULL;
-    }
-#endif
-    return (lalloc((long_u)size, TRUE));
-}
-
-/*
  * Allocate memory like lalloc() and set all bytes to zero.
  */
     char_u *
-lalloc_clear(long_u size, int message)
+lalloc_clear(size_t size, int message)
 {
     char_u *p;
 
     p = (lalloc(size, message));
     if (p != NULL)
-	(void)vim_memset(p, 0, (size_t)size);
+	(void)vim_memset(p, 0, size);
     return p;
 }
 
@@ -904,21 +886,21 @@ lalloc_clear(long_u size, int message)
  * This is used often, KEEP IT FAST!
  */
     char_u *
-lalloc(long_u size, int message)
+lalloc(size_t size, int message)
 {
     char_u	*p;		    /* pointer to new storage space */
     static int	releasing = FALSE;  /* don't do mf_release_all() recursive */
     int		try_again;
 #if defined(HAVE_AVAIL_MEM)
-    static long_u allocated = 0;    /* allocated since last avail check */
+    static size_t allocated = 0;    /* allocated since last avail check */
 #endif
 
-    /* Safety check for allocating zero bytes */
+    // Safety check for allocating zero bytes
     if (size == 0)
     {
-	/* Don't hide this message */
+	// Don't hide this message
 	emsg_silent = 0;
-	siemsg(_("E341: Internal error: lalloc(%ld, )"), size);
+	iemsg(_("E341: Internal error: lalloc(0, )"));
 	return NULL;
     }
 
@@ -939,7 +921,7 @@ lalloc(long_u size, int message)
 	 *    allocating KEEP_ROOM amount of memory.
 	 * 3. Strict check for available memory: call mch_avail_mem()
 	 */
-	if ((p = (char_u *)malloc((size_t)size)) != NULL)
+	if ((p = (char_u *)malloc(size)) != NULL)
 	{
 #ifndef HAVE_AVAIL_MEM
 	    /* 1. No check for available memory: Just return. */
@@ -983,7 +965,7 @@ lalloc(long_u size, int message)
 
 theend:
 #ifdef MEM_PROFILE
-    mem_post_alloc((void **)&p, (size_t)size);
+    mem_post_alloc((void **)&p, size);
 #endif
     return p;
 }
@@ -993,13 +975,13 @@ theend:
  */
 #if defined(FEAT_SIGNS) || defined(PROTO)
     char_u *
-lalloc_id(long_u size, int message, alloc_id_T id UNUSED)
+lalloc_id(size_t size, int message, alloc_id_T id UNUSED)
 {
 #ifdef FEAT_EVAL
     if (alloc_fail_id == id && alloc_does_fail(size))
 	return NULL;
 #endif
-    return (lalloc((long_u)size, message));
+    return (lalloc(size, message));
 }
 #endif
 
@@ -1028,7 +1010,7 @@ mem_realloc(void *ptr, size_t size)
 * Did_outofmem_msg is reset when a character is read.
 */
     void
-do_outofmem_msg(long_u size)
+do_outofmem_msg(size_t size)
 {
     if (!did_outofmem_msg)
     {
@@ -1039,7 +1021,7 @@ do_outofmem_msg(long_u size)
 	 * message fails, e.g. when setting v:errmsg. */
 	did_outofmem_msg = TRUE;
 
-	semsg(_("E342: Out of memory!  (allocating %lu bytes)"), size);
+	semsg(_("E342: Out of memory!  (allocating %lu bytes)"), (long_u)size);
     }
 }
 
@@ -1288,12 +1270,12 @@ free_all_mem(void)
 vim_strsave(char_u *string)
 {
     char_u	*p;
-    unsigned	len;
-
-    len = (unsigned)STRLEN(string) + 1;
+    size_t	len;
+
+    len = STRLEN(string) + 1;
     p = alloc(len);
     if (p != NULL)
-	mch_memmove(p, string, (size_t)len);
+	mch_memmove(p, string, len);
     return p;
 }
 
@@ -1308,7 +1290,7 @@ vim_strnsave(char_u *string, int len)
 {
     char_u	*p;
 
-    p = alloc((unsigned)(len + 1));
+    p = alloc((size_t)(len + 1));
     if (p != NULL)
     {
 	STRNCPY(p, string, len);
@@ -1322,12 +1304,12 @@ vim_strnsave(char_u *string, int len)
  * Returns NULL when out of memory.
  */
     char_u *
-vim_memsave(char_u *p, int len)
+vim_memsave(char_u *p, size_t len)
 {
-    char_u *ret = alloc((unsigned)len);
+    char_u *ret = alloc(len);
 
     if (ret != NULL)
-	mch_memmove(ret, p, (size_t)len);
+	mch_memmove(ret, p, len);
     return ret;
 }
 
@@ -1622,7 +1604,7 @@ strup_save(char_u *orig)
 		newl = utf_char2len(uc);
 		if (newl != l)
 		{
-		    s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
+		    s = alloc(STRLEN(res) + 1 + newl - l);
 		    if (s == NULL)
 		    {
 			vim_free(res);
@@ -1689,7 +1671,7 @@ strlow_save(char_u *orig)
 		newl = utf_char2len(lc);
 		if (newl != l)
 		{
-		    s = alloc((unsigned)STRLEN(res) + 1 + newl - l);
+		    s = alloc(STRLEN(res) + 1 + newl - l);
 		    if (s == NULL)
 		    {
 			vim_free(res);
@@ -2077,7 +2059,7 @@ ga_grow(garray_T *gap, int n)
 	    n = gap->ga_growsize;
 	new_len = gap->ga_itemsize * (gap->ga_len + n);
 	pp = (gap->ga_data == NULL)
-	      ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len);
+	      ? alloc(new_len) : vim_realloc(gap->ga_data, new_len);
 	if (pp == NULL)
 	    return FAIL;
 	old_len = gap->ga_itemsize * gap->ga_maxlen;
@@ -3261,7 +3243,7 @@ call_shell(char_u *cmd, int opt)
 		if (ecmd == NULL)
 		    ecmd = cmd;
 	    }
-	    ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1));
+	    ncmd = alloc(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1);
 	    if (ncmd != NULL)
 	    {
 		STRCPY(ncmd, p_sxq);
@@ -3896,7 +3878,7 @@ qsort(
     int		i, j;
     int		gap;
 
-    buf = alloc((unsigned)elm_size);
+    buf = alloc(elm_size);
     if (buf == NULL)
 	return;
 
@@ -4073,7 +4055,7 @@ putenv(const char *string)
 	    if (moreenv() < 0)
 		return -1;
 	}
-	p = (char *)alloc((unsigned)(strlen(string) + 1));
+	p = (char *)alloc(strlen(string) + 1);
 	if (p == NULL)		/* not enough core */
 	    return -1;
 	environ[i + 1] = 0;	/* new end of env. */
@@ -4121,13 +4103,13 @@ newenv(void)
 	;
 
     esize = i + EXTRASIZE + 1;
-    env = (char **)alloc((unsigned)(esize * sizeof (elem)));
+    env = (char **)alloc(esize * sizeof (elem));
     if (env == NULL)
 	return -1;
 
     for (i = 0; environ[i]; i++)
     {
-	elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1));
+	elem = (char *)alloc(strlen(environ[i]) + 1);
 	if (elem == NULL)
 	    return -1;
 	env[i] = elem;
@@ -4310,7 +4292,7 @@ read_string(FILE *fd, int cnt)
     int		c;
 
     /* allocate memory */
-    str = alloc((unsigned)cnt + 1);
+    str = alloc(cnt + 1);
     if (str != NULL)
     {
 	/* Read the string.  Quit when running into the EOF. */
@@ -4593,7 +4575,7 @@ mch_parse_cmd(char_u *cmd, int use_shcf,
 		}
 	    }
 
-	    *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *)));
+	    *argv = (char **)alloc((*argc + 4) * sizeof(char *));
 	    if (*argv == NULL)	    /* out of memory */
 		return FAIL;
 	}