comparison 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
comparison
equal deleted inserted replaced
16763:fccf84413b53 16764:ef00b6bc186b
709 { 709 {
710 *sizep += sizeof(size_t); 710 *sizep += sizeof(size_t);
711 } 711 }
712 712
713 static void 713 static void
714 mem_pre_alloc_l(long_u *sizep) 714 mem_pre_alloc_l(size_t *sizep)
715 { 715 {
716 *sizep += sizeof(size_t); 716 *sizep += sizeof(size_t);
717 } 717 }
718 718
719 static void 719 static void
794 794
795 #endif /* MEM_PROFILE */ 795 #endif /* MEM_PROFILE */
796 796
797 #ifdef FEAT_EVAL 797 #ifdef FEAT_EVAL
798 int 798 int
799 alloc_does_fail(long_u size) 799 alloc_does_fail(size_t size)
800 { 800 {
801 if (alloc_fail_countdown == 0) 801 if (alloc_fail_countdown == 0)
802 { 802 {
803 if (--alloc_fail_repeat <= 0) 803 if (--alloc_fail_repeat <= 0)
804 alloc_fail_id = 0; 804 alloc_fail_id = 0;
816 */ 816 */
817 #define KEEP_ROOM (2 * 8192L) 817 #define KEEP_ROOM (2 * 8192L)
818 #define KEEP_ROOM_KB (KEEP_ROOM / 1024L) 818 #define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
819 819
820 /* 820 /*
821 * Note: if unsigned is 16 bits we can only allocate up to 64K with alloc(). 821 * The normal way to allocate memory. This handles an out-of-memory situation
822 * Use lalloc for larger blocks. 822 * as well as possible, still returns NULL when we're completely out.
823 */ 823 */
824 char_u * 824 char_u *
825 alloc(unsigned size) 825 alloc(size_t size)
826 { 826 {
827 return (lalloc((long_u)size, TRUE)); 827 return lalloc(size, TRUE);
828 } 828 }
829 829
830 /* 830 /*
831 * alloc() with an ID for alloc_fail(). 831 * alloc() with an ID for alloc_fail().
832 */ 832 */
833 char_u * 833 char_u *
834 alloc_id(unsigned size, alloc_id_T id UNUSED) 834 alloc_id(size_t size, alloc_id_T id UNUSED)
835 { 835 {
836 #ifdef FEAT_EVAL 836 #ifdef FEAT_EVAL
837 if (alloc_fail_id == id && alloc_does_fail((long_u)size)) 837 if (alloc_fail_id == id && alloc_does_fail(size))
838 return NULL; 838 return NULL;
839 #endif 839 #endif
840 return (lalloc((long_u)size, TRUE)); 840 return lalloc(size, TRUE);
841 } 841 }
842 842
843 /* 843 /*
844 * Allocate memory and set all bytes to zero. 844 * Allocate memory and set all bytes to zero.
845 */ 845 */
846 char_u * 846 char_u *
847 alloc_clear(unsigned size) 847 alloc_clear(size_t size)
848 { 848 {
849 char_u *p; 849 char_u *p;
850 850
851 p = lalloc((long_u)size, TRUE); 851 p = lalloc(size, TRUE);
852 if (p != NULL) 852 if (p != NULL)
853 (void)vim_memset(p, 0, (size_t)size); 853 (void)vim_memset(p, 0, size);
854 return p; 854 return p;
855 } 855 }
856 856
857 /* 857 /*
858 * Same as alloc_clear() but with allocation id for testing 858 * Same as alloc_clear() but with allocation id for testing
859 */ 859 */
860 char_u * 860 char_u *
861 alloc_clear_id(unsigned size, alloc_id_T id UNUSED) 861 alloc_clear_id(size_t size, alloc_id_T id UNUSED)
862 { 862 {
863 #ifdef FEAT_EVAL 863 #ifdef FEAT_EVAL
864 if (alloc_fail_id == id && alloc_does_fail((long_u)size)) 864 if (alloc_fail_id == id && alloc_does_fail(size))
865 return NULL; 865 return NULL;
866 #endif 866 #endif
867 return alloc_clear(size); 867 return alloc_clear(size);
868 } 868 }
869 869
870 /* 870 /*
871 * alloc() with check for maximum line length 871 * Allocate memory like lalloc() and set all bytes to zero.
872 */ 872 */
873 char_u * 873 char_u *
874 alloc_check(unsigned size) 874 lalloc_clear(size_t size, int message)
875 {
876 #if !defined(UNIX)
877 if (sizeof(int) == 2 && size > 0x7fff)
878 {
879 /* Don't hide this message */
880 emsg_silent = 0;
881 emsg(_("E340: Line is becoming too long"));
882 return NULL;
883 }
884 #endif
885 return (lalloc((long_u)size, TRUE));
886 }
887
888 /*
889 * Allocate memory like lalloc() and set all bytes to zero.
890 */
891 char_u *
892 lalloc_clear(long_u size, int message)
893 { 875 {
894 char_u *p; 876 char_u *p;
895 877
896 p = (lalloc(size, message)); 878 p = (lalloc(size, message));
897 if (p != NULL) 879 if (p != NULL)
898 (void)vim_memset(p, 0, (size_t)size); 880 (void)vim_memset(p, 0, size);
899 return p; 881 return p;
900 } 882 }
901 883
902 /* 884 /*
903 * Low level memory allocation function. 885 * Low level memory allocation function.
904 * This is used often, KEEP IT FAST! 886 * This is used often, KEEP IT FAST!
905 */ 887 */
906 char_u * 888 char_u *
907 lalloc(long_u size, int message) 889 lalloc(size_t size, int message)
908 { 890 {
909 char_u *p; /* pointer to new storage space */ 891 char_u *p; /* pointer to new storage space */
910 static int releasing = FALSE; /* don't do mf_release_all() recursive */ 892 static int releasing = FALSE; /* don't do mf_release_all() recursive */
911 int try_again; 893 int try_again;
912 #if defined(HAVE_AVAIL_MEM) 894 #if defined(HAVE_AVAIL_MEM)
913 static long_u allocated = 0; /* allocated since last avail check */ 895 static size_t allocated = 0; /* allocated since last avail check */
914 #endif 896 #endif
915 897
916 /* Safety check for allocating zero bytes */ 898 // Safety check for allocating zero bytes
917 if (size == 0) 899 if (size == 0)
918 { 900 {
919 /* Don't hide this message */ 901 // Don't hide this message
920 emsg_silent = 0; 902 emsg_silent = 0;
921 siemsg(_("E341: Internal error: lalloc(%ld, )"), size); 903 iemsg(_("E341: Internal error: lalloc(0, )"));
922 return NULL; 904 return NULL;
923 } 905 }
924 906
925 #ifdef MEM_PROFILE 907 #ifdef MEM_PROFILE
926 mem_pre_alloc_l(&size); 908 mem_pre_alloc_l(&size);
937 * 1. No check for available memory: Just return. 919 * 1. No check for available memory: Just return.
938 * 2. Slow check for available memory: call mch_avail_mem() after 920 * 2. Slow check for available memory: call mch_avail_mem() after
939 * allocating KEEP_ROOM amount of memory. 921 * allocating KEEP_ROOM amount of memory.
940 * 3. Strict check for available memory: call mch_avail_mem() 922 * 3. Strict check for available memory: call mch_avail_mem()
941 */ 923 */
942 if ((p = (char_u *)malloc((size_t)size)) != NULL) 924 if ((p = (char_u *)malloc(size)) != NULL)
943 { 925 {
944 #ifndef HAVE_AVAIL_MEM 926 #ifndef HAVE_AVAIL_MEM
945 /* 1. No check for available memory: Just return. */ 927 /* 1. No check for available memory: Just return. */
946 goto theend; 928 goto theend;
947 #else 929 #else
981 if (message && p == NULL) 963 if (message && p == NULL)
982 do_outofmem_msg(size); 964 do_outofmem_msg(size);
983 965
984 theend: 966 theend:
985 #ifdef MEM_PROFILE 967 #ifdef MEM_PROFILE
986 mem_post_alloc((void **)&p, (size_t)size); 968 mem_post_alloc((void **)&p, size);
987 #endif 969 #endif
988 return p; 970 return p;
989 } 971 }
990 972
991 /* 973 /*
992 * lalloc() with an ID for alloc_fail(). 974 * lalloc() with an ID for alloc_fail().
993 */ 975 */
994 #if defined(FEAT_SIGNS) || defined(PROTO) 976 #if defined(FEAT_SIGNS) || defined(PROTO)
995 char_u * 977 char_u *
996 lalloc_id(long_u size, int message, alloc_id_T id UNUSED) 978 lalloc_id(size_t size, int message, alloc_id_T id UNUSED)
997 { 979 {
998 #ifdef FEAT_EVAL 980 #ifdef FEAT_EVAL
999 if (alloc_fail_id == id && alloc_does_fail(size)) 981 if (alloc_fail_id == id && alloc_does_fail(size))
1000 return NULL; 982 return NULL;
1001 #endif 983 #endif
1002 return (lalloc((long_u)size, message)); 984 return (lalloc(size, message));
1003 } 985 }
1004 #endif 986 #endif
1005 987
1006 #if defined(MEM_PROFILE) || defined(PROTO) 988 #if defined(MEM_PROFILE) || defined(PROTO)
1007 /* 989 /*
1026 /* 1008 /*
1027 * Avoid repeating the error message many times (they take 1 second each). 1009 * Avoid repeating the error message many times (they take 1 second each).
1028 * Did_outofmem_msg is reset when a character is read. 1010 * Did_outofmem_msg is reset when a character is read.
1029 */ 1011 */
1030 void 1012 void
1031 do_outofmem_msg(long_u size) 1013 do_outofmem_msg(size_t size)
1032 { 1014 {
1033 if (!did_outofmem_msg) 1015 if (!did_outofmem_msg)
1034 { 1016 {
1035 /* Don't hide this message */ 1017 /* Don't hide this message */
1036 emsg_silent = 0; 1018 emsg_silent = 0;
1037 1019
1038 /* Must come first to avoid coming back here when printing the error 1020 /* Must come first to avoid coming back here when printing the error
1039 * message fails, e.g. when setting v:errmsg. */ 1021 * message fails, e.g. when setting v:errmsg. */
1040 did_outofmem_msg = TRUE; 1022 did_outofmem_msg = TRUE;
1041 1023
1042 semsg(_("E342: Out of memory! (allocating %lu bytes)"), size); 1024 semsg(_("E342: Out of memory! (allocating %lu bytes)"), (long_u)size);
1043 } 1025 }
1044 } 1026 }
1045 1027
1046 #if defined(EXITFREE) || defined(PROTO) 1028 #if defined(EXITFREE) || defined(PROTO)
1047 1029
1286 */ 1268 */
1287 char_u * 1269 char_u *
1288 vim_strsave(char_u *string) 1270 vim_strsave(char_u *string)
1289 { 1271 {
1290 char_u *p; 1272 char_u *p;
1291 unsigned len; 1273 size_t len;
1292 1274
1293 len = (unsigned)STRLEN(string) + 1; 1275 len = STRLEN(string) + 1;
1294 p = alloc(len); 1276 p = alloc(len);
1295 if (p != NULL) 1277 if (p != NULL)
1296 mch_memmove(p, string, (size_t)len); 1278 mch_memmove(p, string, len);
1297 return p; 1279 return p;
1298 } 1280 }
1299 1281
1300 /* 1282 /*
1301 * Copy up to "len" bytes of "string" into newly allocated memory and 1283 * Copy up to "len" bytes of "string" into newly allocated memory and
1306 char_u * 1288 char_u *
1307 vim_strnsave(char_u *string, int len) 1289 vim_strnsave(char_u *string, int len)
1308 { 1290 {
1309 char_u *p; 1291 char_u *p;
1310 1292
1311 p = alloc((unsigned)(len + 1)); 1293 p = alloc((size_t)(len + 1));
1312 if (p != NULL) 1294 if (p != NULL)
1313 { 1295 {
1314 STRNCPY(p, string, len); 1296 STRNCPY(p, string, len);
1315 p[len] = NUL; 1297 p[len] = NUL;
1316 } 1298 }
1320 /* 1302 /*
1321 * Copy "p[len]" into allocated memory, ignoring NUL characters. 1303 * Copy "p[len]" into allocated memory, ignoring NUL characters.
1322 * Returns NULL when out of memory. 1304 * Returns NULL when out of memory.
1323 */ 1305 */
1324 char_u * 1306 char_u *
1325 vim_memsave(char_u *p, int len) 1307 vim_memsave(char_u *p, size_t len)
1326 { 1308 {
1327 char_u *ret = alloc((unsigned)len); 1309 char_u *ret = alloc(len);
1328 1310
1329 if (ret != NULL) 1311 if (ret != NULL)
1330 mch_memmove(ret, p, (size_t)len); 1312 mch_memmove(ret, p, len);
1331 return ret; 1313 return ret;
1332 } 1314 }
1333 1315
1334 /* 1316 /*
1335 * Same as vim_strsave(), but any characters found in esc_chars are preceded 1317 * Same as vim_strsave(), but any characters found in esc_chars are preceded
1620 /* Reallocate string when byte count changes. This is rare, 1602 /* Reallocate string when byte count changes. This is rare,
1621 * thus it's OK to do another malloc()/free(). */ 1603 * thus it's OK to do another malloc()/free(). */
1622 newl = utf_char2len(uc); 1604 newl = utf_char2len(uc);
1623 if (newl != l) 1605 if (newl != l)
1624 { 1606 {
1625 s = alloc((unsigned)STRLEN(res) + 1 + newl - l); 1607 s = alloc(STRLEN(res) + 1 + newl - l);
1626 if (s == NULL) 1608 if (s == NULL)
1627 { 1609 {
1628 vim_free(res); 1610 vim_free(res);
1629 return NULL; 1611 return NULL;
1630 } 1612 }
1687 /* Reallocate string when byte count changes. This is rare, 1669 /* Reallocate string when byte count changes. This is rare,
1688 * thus it's OK to do another malloc()/free(). */ 1670 * thus it's OK to do another malloc()/free(). */
1689 newl = utf_char2len(lc); 1671 newl = utf_char2len(lc);
1690 if (newl != l) 1672 if (newl != l)
1691 { 1673 {
1692 s = alloc((unsigned)STRLEN(res) + 1 + newl - l); 1674 s = alloc(STRLEN(res) + 1 + newl - l);
1693 if (s == NULL) 1675 if (s == NULL)
1694 { 1676 {
1695 vim_free(res); 1677 vim_free(res);
1696 return NULL; 1678 return NULL;
1697 } 1679 }
2075 { 2057 {
2076 if (n < gap->ga_growsize) 2058 if (n < gap->ga_growsize)
2077 n = gap->ga_growsize; 2059 n = gap->ga_growsize;
2078 new_len = gap->ga_itemsize * (gap->ga_len + n); 2060 new_len = gap->ga_itemsize * (gap->ga_len + n);
2079 pp = (gap->ga_data == NULL) 2061 pp = (gap->ga_data == NULL)
2080 ? alloc((unsigned)new_len) : vim_realloc(gap->ga_data, new_len); 2062 ? alloc(new_len) : vim_realloc(gap->ga_data, new_len);
2081 if (pp == NULL) 2063 if (pp == NULL)
2082 return FAIL; 2064 return FAIL;
2083 old_len = gap->ga_itemsize * gap->ga_maxlen; 2065 old_len = gap->ga_itemsize * gap->ga_maxlen;
2084 vim_memset(pp + old_len, 0, new_len - old_len); 2066 vim_memset(pp + old_len, 0, new_len - old_len);
2085 gap->ga_maxlen = gap->ga_len + n; 2067 gap->ga_maxlen = gap->ga_len + n;
3259 { 3241 {
3260 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE); 3242 ecmd = vim_strsave_escaped_ext(cmd, p_sxe, '^', FALSE);
3261 if (ecmd == NULL) 3243 if (ecmd == NULL)
3262 ecmd = cmd; 3244 ecmd = cmd;
3263 } 3245 }
3264 ncmd = alloc((unsigned)(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1)); 3246 ncmd = alloc(STRLEN(ecmd) + STRLEN(p_sxq) * 2 + 1);
3265 if (ncmd != NULL) 3247 if (ncmd != NULL)
3266 { 3248 {
3267 STRCPY(ncmd, p_sxq); 3249 STRCPY(ncmd, p_sxq);
3268 STRCAT(ncmd, ecmd); 3250 STRCAT(ncmd, ecmd);
3269 /* When 'shellxquote' is ( append ). 3251 /* When 'shellxquote' is ( append ).
3894 char_u *p1; 3876 char_u *p1;
3895 char_u *p2; 3877 char_u *p2;
3896 int i, j; 3878 int i, j;
3897 int gap; 3879 int gap;
3898 3880
3899 buf = alloc((unsigned)elm_size); 3881 buf = alloc(elm_size);
3900 if (buf == NULL) 3882 if (buf == NULL)
3901 return; 3883 return;
3902 3884
3903 for (gap = elm_count / 2; gap > 0; gap /= 2) 3885 for (gap = elm_count / 2; gap > 0; gap /= 2)
3904 for (i = gap; i < elm_count; ++i) 3886 for (i = gap; i < elm_count; ++i)
4071 if (i >= (envsize - 1)) 4053 if (i >= (envsize - 1))
4072 { /* need new slot */ 4054 { /* need new slot */
4073 if (moreenv() < 0) 4055 if (moreenv() < 0)
4074 return -1; 4056 return -1;
4075 } 4057 }
4076 p = (char *)alloc((unsigned)(strlen(string) + 1)); 4058 p = (char *)alloc(strlen(string) + 1);
4077 if (p == NULL) /* not enough core */ 4059 if (p == NULL) /* not enough core */
4078 return -1; 4060 return -1;
4079 environ[i + 1] = 0; /* new end of env. */ 4061 environ[i + 1] = 0; /* new end of env. */
4080 } 4062 }
4081 else 4063 else
4119 4101
4120 for (i = 0; environ[i]; i++) 4102 for (i = 0; environ[i]; i++)
4121 ; 4103 ;
4122 4104
4123 esize = i + EXTRASIZE + 1; 4105 esize = i + EXTRASIZE + 1;
4124 env = (char **)alloc((unsigned)(esize * sizeof (elem))); 4106 env = (char **)alloc(esize * sizeof (elem));
4125 if (env == NULL) 4107 if (env == NULL)
4126 return -1; 4108 return -1;
4127 4109
4128 for (i = 0; environ[i]; i++) 4110 for (i = 0; environ[i]; i++)
4129 { 4111 {
4130 elem = (char *)alloc((unsigned)(strlen(environ[i]) + 1)); 4112 elem = (char *)alloc(strlen(environ[i]) + 1);
4131 if (elem == NULL) 4113 if (elem == NULL)
4132 return -1; 4114 return -1;
4133 env[i] = elem; 4115 env[i] = elem;
4134 strcpy(elem, environ[i]); 4116 strcpy(elem, environ[i]);
4135 } 4117 }
4308 char_u *str; 4290 char_u *str;
4309 int i; 4291 int i;
4310 int c; 4292 int c;
4311 4293
4312 /* allocate memory */ 4294 /* allocate memory */
4313 str = alloc((unsigned)cnt + 1); 4295 str = alloc(cnt + 1);
4314 if (str != NULL) 4296 if (str != NULL)
4315 { 4297 {
4316 /* Read the string. Quit when running into the EOF. */ 4298 /* Read the string. Quit when running into the EOF. */
4317 for (i = 0; i < cnt; ++i) 4299 for (i = 0; i < cnt; ++i)
4318 { 4300 {
4591 ++*argc; 4573 ++*argc;
4592 p = skipwhite(p); 4574 p = skipwhite(p);
4593 } 4575 }
4594 } 4576 }
4595 4577
4596 *argv = (char **)alloc((unsigned)((*argc + 4) * sizeof(char *))); 4578 *argv = (char **)alloc((*argc + 4) * sizeof(char *));
4597 if (*argv == NULL) /* out of memory */ 4579 if (*argv == NULL) /* out of memory */
4598 return FAIL; 4580 return FAIL;
4599 } 4581 }
4600 } 4582 }
4601 return OK; 4583 return OK;