Mercurial > emacs
comparison src/alloc.c @ 21258:693573ac0944
(make_specified_string): New function.
(make_string_from_bytes): New function.
(compact_strings): Get byte size from size, if size_byte < 0.
(Fmake_string): Use make_uninit_string for single-byte char.
(make_unibyte_string): Mark string as unibyte.
(make_uninit_string): Likewise.
(make_string): Likewise, if size == size in bytes.
(make_pure_string): New arg MULTIBYTE.
(Fpurecopy): Pass new arg to make_pure_string.
| author | Richard M. Stallman <rms@gnu.org> |
|---|---|
| date | Sat, 21 Mar 1998 18:07:06 +0000 |
| parents | 50929073a0ba |
| children | dc2cbd40703c |
comparison
equal
deleted
inserted
replaced
| 21257:205a5aa4aa2f | 21258:693573ac0944 |
|---|---|
| 1177 current_string_block->next = 0; | 1177 current_string_block->next = 0; |
| 1178 current_string_block->prev = 0; | 1178 current_string_block->prev = 0; |
| 1179 current_string_block->pos = 0; | 1179 current_string_block->pos = 0; |
| 1180 large_string_blocks = 0; | 1180 large_string_blocks = 0; |
| 1181 } | 1181 } |
| 1182 | 1182 |
| 1183 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0, | 1183 DEFUN ("make-string", Fmake_string, Smake_string, 2, 2, 0, |
| 1184 "Return a newly created string of length LENGTH, with each element being INIT.\n\ | 1184 "Return a newly created string of length LENGTH, with each element being INIT.\n\ |
| 1185 Both LENGTH and INIT must be numbers.") | 1185 Both LENGTH and INIT must be numbers.") |
| 1186 (length, init) | 1186 (length, init) |
| 1187 Lisp_Object length, init; | 1187 Lisp_Object length, init; |
| 1195 | 1195 |
| 1196 c = XINT (init); | 1196 c = XINT (init); |
| 1197 if (SINGLE_BYTE_CHAR_P (c)) | 1197 if (SINGLE_BYTE_CHAR_P (c)) |
| 1198 { | 1198 { |
| 1199 nbytes = XINT (length); | 1199 nbytes = XINT (length); |
| 1200 val = make_uninit_multibyte_string (nbytes, nbytes); | 1200 val = make_uninit_string (nbytes); |
| 1201 p = XSTRING (val)->data; | 1201 p = XSTRING (val)->data; |
| 1202 end = p + XSTRING (val)->size; | 1202 end = p + XSTRING (val)->size; |
| 1203 while (p != end) | 1203 while (p != end) |
| 1204 *p++ = c; | 1204 *p++ = c; |
| 1205 } | 1205 } |
| 1253 for (i = 0; i < length_in_chars ; i++) | 1253 for (i = 0; i < length_in_chars ; i++) |
| 1254 p->data[i] = real_init; | 1254 p->data[i] = real_init; |
| 1255 | 1255 |
| 1256 return val; | 1256 return val; |
| 1257 } | 1257 } |
| 1258 | 1258 |
| 1259 /* Make a string from NBYTES bytes at CONTENTS, | 1259 /* Make a string from NBYTES bytes at CONTENTS, |
| 1260 and compute the number of characters from the contents. */ | 1260 and compute the number of characters from the contents. |
| 1261 This string may be unibyte or multibyte, depending on the contents. */ | |
| 1261 | 1262 |
| 1262 Lisp_Object | 1263 Lisp_Object |
| 1263 make_string (contents, nbytes) | 1264 make_string (contents, nbytes) |
| 1264 char *contents; | 1265 char *contents; |
| 1265 int nbytes; | 1266 int nbytes; |
| 1266 { | 1267 { |
| 1267 register Lisp_Object val; | 1268 register Lisp_Object val; |
| 1268 int nchars = chars_in_text (contents, nbytes); | 1269 int nchars = chars_in_text (contents, nbytes); |
| 1269 val = make_uninit_multibyte_string (nchars, nbytes); | 1270 val = make_uninit_multibyte_string (nchars, nbytes); |
| 1270 bcopy (contents, XSTRING (val)->data, nbytes); | 1271 bcopy (contents, XSTRING (val)->data, nbytes); |
| 1272 if (STRING_BYTES (XSTRING (val)) == XSTRING (val)->size) | |
| 1273 SET_STRING_BYTES (XSTRING (val), -1); | |
| 1271 return val; | 1274 return val; |
| 1272 } | 1275 } |
| 1273 | 1276 |
| 1274 /* Make a string from LENGTH bytes at CONTENTS, | 1277 /* Make a unibyte string from LENGTH bytes at CONTENTS. */ |
| 1275 assuming each byte is a character. */ | |
| 1276 | 1278 |
| 1277 Lisp_Object | 1279 Lisp_Object |
| 1278 make_unibyte_string (contents, length) | 1280 make_unibyte_string (contents, length) |
| 1279 char *contents; | 1281 char *contents; |
| 1280 int length; | 1282 int length; |
| 1281 { | 1283 { |
| 1282 register Lisp_Object val; | 1284 register Lisp_Object val; |
| 1283 val = make_uninit_string (length); | 1285 val = make_uninit_string (length); |
| 1284 bcopy (contents, XSTRING (val)->data, length); | 1286 bcopy (contents, XSTRING (val)->data, length); |
| 1287 SET_STRING_BYTES (XSTRING (val), -1); | |
| 1285 return val; | 1288 return val; |
| 1286 } | 1289 } |
| 1287 | 1290 |
| 1288 /* Make a string from NCHARS characters occupying NBYTES bytes at CONTENTS. */ | 1291 /* Make a multibyte string from NCHARS characters |
| 1292 occupying NBYTES bytes at CONTENTS. */ | |
| 1289 | 1293 |
| 1290 Lisp_Object | 1294 Lisp_Object |
| 1291 make_multibyte_string (contents, nchars, nbytes) | 1295 make_multibyte_string (contents, nchars, nbytes) |
| 1292 char *contents; | 1296 char *contents; |
| 1293 int nchars, nbytes; | 1297 int nchars, nbytes; |
| 1296 val = make_uninit_multibyte_string (nchars, nbytes); | 1300 val = make_uninit_multibyte_string (nchars, nbytes); |
| 1297 bcopy (contents, XSTRING (val)->data, nbytes); | 1301 bcopy (contents, XSTRING (val)->data, nbytes); |
| 1298 return val; | 1302 return val; |
| 1299 } | 1303 } |
| 1300 | 1304 |
| 1305 /* Make a string from NCHARS characters | |
| 1306 occupying NBYTES bytes at CONTENTS. | |
| 1307 It is a multibyte string if NBYTES != NCHARS. */ | |
| 1308 | |
| 1309 Lisp_Object | |
| 1310 make_string_from_bytes (contents, nchars, nbytes) | |
| 1311 char *contents; | |
| 1312 int nchars, nbytes; | |
| 1313 { | |
| 1314 register Lisp_Object val; | |
| 1315 val = make_uninit_multibyte_string (nchars, nbytes); | |
| 1316 bcopy (contents, XSTRING (val)->data, nbytes); | |
| 1317 if (STRING_BYTES (XSTRING (val)) == XSTRING (val)->size) | |
| 1318 SET_STRING_BYTES (XSTRING (val), -1); | |
| 1319 return val; | |
| 1320 } | |
| 1321 | |
| 1322 /* Make a multibyte string from NCHARS characters | |
| 1323 occupying NBYTES bytes at CONTENTS. */ | |
| 1324 | |
| 1325 Lisp_Object | |
| 1326 make_specified_string (contents, nchars, nbytes, multibyte) | |
| 1327 char *contents; | |
| 1328 int nchars, nbytes; | |
| 1329 int multibyte; | |
| 1330 { | |
| 1331 register Lisp_Object val; | |
| 1332 val = make_uninit_multibyte_string (nchars, nbytes); | |
| 1333 bcopy (contents, XSTRING (val)->data, nbytes); | |
| 1334 if (!multibyte) | |
| 1335 SET_STRING_BYTES (XSTRING (val), -1); | |
| 1336 return val; | |
| 1337 } | |
| 1338 | |
| 1301 /* Make a string from the data at STR, | 1339 /* Make a string from the data at STR, |
| 1302 treating it as multibyte if the data warrants. */ | 1340 treating it as multibyte if the data warrants. */ |
| 1303 | 1341 |
| 1304 Lisp_Object | 1342 Lisp_Object |
| 1305 build_string (str) | 1343 build_string (str) |
| 1306 char *str; | 1344 char *str; |
| 1307 { | 1345 { |
| 1308 return make_string (str, strlen (str)); | 1346 return make_string (str, strlen (str)); |
| 1309 } | 1347 } |
| 1310 | 1348 |
| 1311 Lisp_Object | 1349 Lisp_Object |
| 1312 make_uninit_string (length) | 1350 make_uninit_string (length) |
| 1313 int length; | 1351 int length; |
| 1314 { | 1352 { |
| 1315 return make_uninit_multibyte_string (length, length); | 1353 Lisp_Object val; |
| 1354 val = make_uninit_multibyte_string (length, length); | |
| 1355 SET_STRING_BYTES (XSTRING (val), -1); | |
| 1356 return val; | |
| 1316 } | 1357 } |
| 1317 | 1358 |
| 1318 Lisp_Object | 1359 Lisp_Object |
| 1319 make_uninit_multibyte_string (length, length_byte) | 1360 make_uninit_multibyte_string (length, length_byte) |
| 1320 int length, length_byte; | 1361 int length, length_byte; |
| 1380 XSTRING (val)->data[length_byte] = 0; | 1421 XSTRING (val)->data[length_byte] = 0; |
| 1381 INITIALIZE_INTERVAL (XSTRING (val), NULL_INTERVAL); | 1422 INITIALIZE_INTERVAL (XSTRING (val), NULL_INTERVAL); |
| 1382 | 1423 |
| 1383 return val; | 1424 return val; |
| 1384 } | 1425 } |
| 1385 | 1426 |
| 1386 /* Return a newly created vector or string with specified arguments as | 1427 /* Return a newly created vector or string with specified arguments as |
| 1387 elements. If all the arguments are characters that can fit | 1428 elements. If all the arguments are characters that can fit |
| 1388 in a string of events, make a string; otherwise, make a vector. | 1429 in a string of events, make a string; otherwise, make a vector. |
| 1389 | 1430 |
| 1390 Any number of arguments, even zero arguments, are allowed. */ | 1431 Any number of arguments, even zero arguments, are allowed. */ |
| 1428 since if it cannot hold a large string | 1469 since if it cannot hold a large string |
| 1429 it may be able to hold conses that point to that string; | 1470 it may be able to hold conses that point to that string; |
| 1430 then the string is not protected from gc. */ | 1471 then the string is not protected from gc. */ |
| 1431 | 1472 |
| 1432 Lisp_Object | 1473 Lisp_Object |
| 1433 make_pure_string (data, length, length_byte) | 1474 make_pure_string (data, length, length_byte, multibyte) |
| 1434 char *data; | 1475 char *data; |
| 1435 int length; | 1476 int length; |
| 1436 int length_byte; | 1477 int length_byte; |
| 1437 { | 1478 int multibyte; |
| 1479 { | |
| 1480 | |
| 1438 register Lisp_Object new; | 1481 register Lisp_Object new; |
| 1439 register int size = STRING_FULLSIZE (length_byte); | 1482 register int size = STRING_FULLSIZE (length_byte); |
| 1440 | 1483 |
| 1441 if (pureptr + size > PURESIZE) | 1484 if (pureptr + size > PURESIZE) |
| 1442 error ("Pure Lisp storage exhausted"); | 1485 error ("Pure Lisp storage exhausted"); |
| 1443 XSETSTRING (new, PUREBEG + pureptr); | 1486 XSETSTRING (new, PUREBEG + pureptr); |
| 1444 XSTRING (new)->size = length; | 1487 XSTRING (new)->size = length; |
| 1445 SET_STRING_BYTES (XSTRING (new), length_byte); | 1488 SET_STRING_BYTES (XSTRING (new), (multibyte ? length_byte : -1)); |
| 1446 bcopy (data, XSTRING (new)->data, length_byte); | 1489 bcopy (data, XSTRING (new)->data, length_byte); |
| 1447 XSTRING (new)->data[length_byte] = 0; | 1490 XSTRING (new)->data[length_byte] = 0; |
| 1448 | 1491 |
| 1449 /* We must give strings in pure storage some kind of interval. So we | 1492 /* We must give strings in pure storage some kind of interval. So we |
| 1450 give them a null one. */ | 1493 give them a null one. */ |
| 1546 else if (FLOATP (obj)) | 1589 else if (FLOATP (obj)) |
| 1547 return make_pure_float (XFLOAT (obj)->data); | 1590 return make_pure_float (XFLOAT (obj)->data); |
| 1548 #endif /* LISP_FLOAT_TYPE */ | 1591 #endif /* LISP_FLOAT_TYPE */ |
| 1549 else if (STRINGP (obj)) | 1592 else if (STRINGP (obj)) |
| 1550 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size, | 1593 return make_pure_string (XSTRING (obj)->data, XSTRING (obj)->size, |
| 1551 STRING_BYTES (XSTRING (obj))); | 1594 STRING_BYTES (XSTRING (obj)), |
| 1595 STRING_MULTIBYTE (obj)); | |
| 1552 else if (COMPILEDP (obj) || VECTORP (obj)) | 1596 else if (COMPILEDP (obj) || VECTORP (obj)) |
| 1553 { | 1597 { |
| 1554 register struct Lisp_Vector *vec; | 1598 register struct Lisp_Vector *vec; |
| 1555 register int i, size; | 1599 register int i, size; |
| 1556 | 1600 |
| 2644 register struct Lisp_String *nextstr | 2688 register struct Lisp_String *nextstr |
| 2645 = (struct Lisp_String *) &from_sb->chars[pos]; | 2689 = (struct Lisp_String *) &from_sb->chars[pos]; |
| 2646 | 2690 |
| 2647 register struct Lisp_String *newaddr; | 2691 register struct Lisp_String *newaddr; |
| 2648 register EMACS_INT size = nextstr->size; | 2692 register EMACS_INT size = nextstr->size; |
| 2649 EMACS_INT size_byte = STRING_BYTES (nextstr); | 2693 EMACS_INT size_byte = nextstr->size_byte; |
| 2650 | 2694 |
| 2651 /* NEXTSTR is the old address of the next string. | 2695 /* NEXTSTR is the old address of the next string. |
| 2652 Just skip it if it isn't marked. */ | 2696 Just skip it if it isn't marked. */ |
| 2653 if (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE) | 2697 if (((EMACS_UINT) size & ~DONT_COPY_FLAG) > STRING_BLOCK_SIZE) |
| 2654 { | 2698 { |
| 2658 { | 2702 { |
| 2659 if (size & DONT_COPY_FLAG) | 2703 if (size & DONT_COPY_FLAG) |
| 2660 size ^= MARKBIT | DONT_COPY_FLAG; | 2704 size ^= MARKBIT | DONT_COPY_FLAG; |
| 2661 size = *(EMACS_INT *)size & ~MARKBIT; | 2705 size = *(EMACS_INT *)size & ~MARKBIT; |
| 2662 } | 2706 } |
| 2707 | |
| 2708 if (size_byte < 0) | |
| 2709 size_byte = size; | |
| 2663 | 2710 |
| 2664 total_string_size += size_byte; | 2711 total_string_size += size_byte; |
| 2665 | 2712 |
| 2666 /* If it won't fit in TO_SB, close it out, | 2713 /* If it won't fit in TO_SB, close it out, |
| 2667 and move to the next sb. Keep doing so until | 2714 and move to the next sb. Keep doing so until |
| 2718 XSETSTRING (* (Lisp_Object *) &newaddr->intervals->parent, | 2765 XSETSTRING (* (Lisp_Object *) &newaddr->intervals->parent, |
| 2719 newaddr); | 2766 newaddr); |
| 2720 } | 2767 } |
| 2721 #endif /* USE_TEXT_PROPERTIES */ | 2768 #endif /* USE_TEXT_PROPERTIES */ |
| 2722 } | 2769 } |
| 2770 else if (size_byte < 0) | |
| 2771 size_byte = size; | |
| 2772 | |
| 2723 pos += STRING_FULLSIZE (size_byte); | 2773 pos += STRING_FULLSIZE (size_byte); |
| 2724 } | 2774 } |
| 2725 } | 2775 } |
| 2726 | 2776 |
| 2727 /* Close out the last string block still used and free any that follow. */ | 2777 /* Close out the last string block still used and free any that follow. */ |
