comparison src/lisp.h @ 3293:168191f9515f

[xemacs-hg @ 2006-03-20 21:20:19 by crestani] 2006-03-20 Marcus Crestani <crestani@xemacs.org> * dynarr.c (Dynarr_realloc): Malloc correct size. * lisp.h: Dynamic array cleanup, remove duplicate code.
author crestani
date Mon, 20 Mar 2006 21:20:29 +0000
parents d674024a8674
children 73051095a712
comparison
equal deleted inserted replaced
3292:b327b25c93bf 3293:168191f9515f
1249 (sizevar) *= 2; \ 1249 (sizevar) *= 2; \
1250 XREALLOC_ARRAY (basevar, type, (sizevar)); \ 1250 XREALLOC_ARRAY (basevar, type, (sizevar)); \
1251 } \ 1251 } \
1252 } while (0) 1252 } while (0)
1253 1253
1254 /* ------------------------ dynamic arrays ------------------- */
1255
1256 #ifndef NEW_GC
1257 #ifdef ERROR_CHECK_STRUCTURES
1258 #define Dynarr_declare(type) \
1259 type *base; \
1260 int locked; \
1261 int elsize; \
1262 int cur; \
1263 int largest; \
1264 int max
1265 #else
1266 #define Dynarr_declare(type) \
1267 type *base; \
1268 int elsize; \
1269 int cur; \
1270 int largest; \
1271 int max
1272 #endif /* ERROR_CHECK_STRUCTURES */
1273
1274 typedef struct dynarr
1275 {
1276 Dynarr_declare (void);
1277 } Dynarr;
1278
1279 MODULE_API void *Dynarr_newf (int elsize);
1280 MODULE_API void Dynarr_resize (void *dy, Elemcount size);
1281 MODULE_API void Dynarr_insert_many (void *d, const void *el, int len, int start);
1282 MODULE_API void Dynarr_delete_many (void *d, int start, int len);
1283 MODULE_API void Dynarr_free (void *d);
1284
1285 #define Dynarr_new(type) ((type##_dynarr *) Dynarr_newf (sizeof (type)))
1286 #define Dynarr_new2(dynarr_type, type) \
1287 ((dynarr_type *) Dynarr_newf (sizeof (type)))
1288 #define Dynarr_at(d, pos) ((d)->base[pos])
1289 #define Dynarr_atp(d, pos) (&Dynarr_at (d, pos))
1290 #define Dynarr_begin(d) Dynarr_atp (d, 0)
1291 #define Dynarr_end(d) Dynarr_atp (d, Dynarr_length (d) - 1)
1292 #define Dynarr_sizeof(d) ((d)->cur * (d)->elsize)
1293
1294 #ifdef ERROR_CHECK_STRUCTURES
1295 DECLARE_INLINE_HEADER (
1296 Dynarr *
1297 Dynarr_verify_1 (void *d, const Ascbyte *file, int line)
1298 )
1299 {
1300 Dynarr *dy = (Dynarr *) d;
1301 assert_at_line (dy->cur >= 0 && dy->cur <= dy->largest &&
1302 dy->largest <= dy->max, file, line);
1303 return dy;
1304 }
1305
1306 DECLARE_INLINE_HEADER (
1307 Dynarr *
1308 Dynarr_verify_mod_1 (void *d, const Ascbyte *file, int line)
1309 )
1310 {
1311 Dynarr *dy = (Dynarr *) d;
1312 assert_at_line (!dy->locked, file, line);
1313 assert_at_line (dy->cur >= 0 && dy->cur <= dy->largest &&
1314 dy->largest <= dy->max, file, line);
1315 return dy;
1316 }
1317
1318 #define Dynarr_verify(d) Dynarr_verify_1 (d, __FILE__, __LINE__)
1319 #define Dynarr_verify_mod(d) Dynarr_verify_mod_1 (d, __FILE__, __LINE__)
1320 #define Dynarr_lock(d) (Dynarr_verify_mod (d)->locked = 1)
1321 #define Dynarr_unlock(d) ((d)->locked = 0)
1322 #else
1323 #define Dynarr_verify(d) (d)
1324 #define Dynarr_verify_mod(d) (d)
1325 #define Dynarr_lock(d)
1326 #define Dynarr_unlock(d)
1327 #endif /* ERROR_CHECK_STRUCTURES */
1328
1329 #define Dynarr_length(d) (Dynarr_verify (d)->cur)
1330 #define Dynarr_largest(d) (Dynarr_verify (d)->largest)
1331 #define Dynarr_reset(d) (Dynarr_verify_mod (d)->cur = 0)
1332 #define Dynarr_add_many(d, el, len) Dynarr_insert_many (d, el, len, (d)->cur)
1333 #define Dynarr_insert_many_at_start(d, el, len) \
1334 Dynarr_insert_many (d, el, len, 0)
1335 #define Dynarr_add_literal_string(d, s) Dynarr_add_many (d, s, sizeof (s) - 1)
1336 #define Dynarr_add_lisp_string(d, s, codesys) \
1337 do { \
1338 Lisp_Object dyna_ls_s = (s); \
1339 Lisp_Object dyna_ls_cs = (codesys); \
1340 Extbyte *dyna_ls_eb; \
1341 Bytecount dyna_ls_bc; \
1342 \
1343 LISP_STRING_TO_SIZED_EXTERNAL (dyna_ls_s, dyna_ls_eb, \
1344 dyna_ls_bc, dyna_ls_cs); \
1345 Dynarr_add_many (d, dyna_ls_eb, dyna_ls_bc); \
1346 } while (0)
1347
1348 #define Dynarr_add(d, el) ( \
1349 Dynarr_verify_mod (d)->cur >= (d)->max ? Dynarr_resize ((d), (d)->cur+1) : \
1350 (void) 0, \
1351 ((d)->base)[(d)->cur++] = (el), \
1352 (d)->cur > (d)->largest ? (d)->largest = (d)->cur : (int) 0)
1353
1354 /* The following defines will get you into real trouble if you aren't
1355 careful. But they can save a lot of execution time when used wisely. */
1356 #define Dynarr_increment(d) (Dynarr_verify_mod (d)->cur++)
1357 #define Dynarr_set_size(d, n) (Dynarr_verify_mod (d)->cur = n)
1358
1359 #define Dynarr_pop(d) \
1360 (assert ((d)->cur > 0), Dynarr_verify_mod (d)->cur--, \
1361 Dynarr_at (d, (d)->cur))
1362 #define Dynarr_delete(d, i) Dynarr_delete_many (d, i, 1)
1363 #define Dynarr_delete_by_pointer(d, p) \
1364 Dynarr_delete_many (d, (p) - ((d)->base), 1)
1365
1366 #define Dynarr_delete_object(d, el) \
1367 do \
1368 { \
1369 REGISTER int i; \
1370 for (i = Dynarr_length (d) - 1; i >= 0; i--) \
1371 { \
1372 if (el == Dynarr_at (d, i)) \
1373 Dynarr_delete_many (d, i, 1); \
1374 } \
1375 } while (0)
1376
1377 #ifdef MEMORY_USAGE_STATS
1378 struct overhead_stats;
1379 Bytecount Dynarr_memory_usage (void *d, struct overhead_stats *stats);
1380 #endif
1381
1382 void *stack_like_malloc (Bytecount size);
1383 void stack_like_free (void *val);
1384 #endif /* not NEW_GC */
1385
1386 /************************************************************************/ 1254 /************************************************************************/
1387 /** Definitions of more complex types **/ 1255 /** Definitions of more complex types **/
1388 /************************************************************************/ 1256 /************************************************************************/
1389 1257
1390 /* Note that the simplest typedefs are near the top of this file. */ 1258 /* Note that the simplest typedefs are near the top of this file. */
1466 typedef struct Lisp_Color_Instance Lisp_Color_Instance; /* objects-impl.h */ 1334 typedef struct Lisp_Color_Instance Lisp_Color_Instance; /* objects-impl.h */
1467 typedef struct Lisp_Font_Instance Lisp_Font_Instance; /* objects-impl.h */ 1335 typedef struct Lisp_Font_Instance Lisp_Font_Instance; /* objects-impl.h */
1468 typedef struct Lisp_Image_Instance Lisp_Image_Instance; /* glyphs.h */ 1336 typedef struct Lisp_Image_Instance Lisp_Image_Instance; /* glyphs.h */
1469 typedef struct Lisp_Gui_Item Lisp_Gui_Item; 1337 typedef struct Lisp_Gui_Item Lisp_Gui_Item;
1470 1338
1471 #ifndef NEW_GC
1472 /* ------------------------------- */
1473 /* Dynarr typedefs */
1474 /* ------------------------------- */
1475
1476 /* Dynarr typedefs -- basic types first */
1477
1478 typedef struct
1479 {
1480 Dynarr_declare (Ibyte);
1481 } Ibyte_dynarr;
1482
1483 typedef struct
1484 {
1485 Dynarr_declare (Extbyte);
1486 } Extbyte_dynarr;
1487
1488 typedef struct
1489 {
1490 Dynarr_declare (Ichar);
1491 } Ichar_dynarr;
1492
1493 typedef struct
1494 {
1495 Dynarr_declare (char);
1496 } char_dynarr;
1497
1498 typedef struct
1499 {
1500 Dynarr_declare (char *);
1501 } char_ptr_dynarr;
1502
1503 typedef unsigned char unsigned_char;
1504 typedef struct
1505 {
1506 Dynarr_declare (unsigned char);
1507 } unsigned_char_dynarr;
1508
1509 typedef unsigned long unsigned_long;
1510 typedef struct
1511 {
1512 Dynarr_declare (unsigned long);
1513 } unsigned_long_dynarr;
1514
1515 typedef struct
1516 {
1517 Dynarr_declare (int);
1518 } int_dynarr;
1519
1520 typedef struct
1521 {
1522 Dynarr_declare (Charbpos);
1523 } Charbpos_dynarr;
1524
1525 typedef struct
1526 {
1527 Dynarr_declare (Bytebpos);
1528 } Bytebpos_dynarr;
1529
1530 typedef struct
1531 {
1532 Dynarr_declare (Charcount);
1533 } Charcount_dynarr;
1534
1535 typedef struct
1536 {
1537 Dynarr_declare (Bytecount);
1538 } Bytecount_dynarr;
1539
1540 /* Dynarr typedefs -- more complex types */
1541
1542 typedef struct
1543 {
1544 Dynarr_declare (struct face_cachel);
1545 } face_cachel_dynarr;
1546
1547 typedef struct
1548 {
1549 Dynarr_declare (struct glyph_cachel);
1550 } glyph_cachel_dynarr;
1551
1552 typedef struct
1553 {
1554 Dynarr_declare (struct console_type_entry);
1555 } console_type_entry_dynarr;
1556 #endif /* not NEW_GC */
1557
1558 /* ------------------------------- */ 1339 /* ------------------------------- */
1559 /* enum typedefs */ 1340 /* enum typedefs */
1560 /* ------------------------------- */ 1341 /* ------------------------------- */
1561 1342
1562 enum run_hooks_condition 1343 enum run_hooks_condition
1668 # include "lisp-disunion.h" 1449 # include "lisp-disunion.h"
1669 #endif /* !USE_UNION_TYPE */ 1450 #endif /* !USE_UNION_TYPE */
1670 1451
1671 #define XPNTR(x) ((void *) XPNTRVAL(x)) 1452 #define XPNTR(x) ((void *) XPNTRVAL(x))
1672 1453
1673 #ifndef NEW_GC
1674 /* WARNING WARNING WARNING. You must ensure on your own that proper
1675 GC protection is provided for the elements in this array. */
1676 typedef struct
1677 {
1678 Dynarr_declare (Lisp_Object);
1679 } Lisp_Object_dynarr;
1680
1681 typedef struct
1682 {
1683 Dynarr_declare (Lisp_Object *);
1684 } Lisp_Object_ptr_dynarr;
1685 #endif /* not NEW_GC */
1686
1687 /* Close your eyes now lest you vomit or spontaneously combust ... */ 1454 /* Close your eyes now lest you vomit or spontaneously combust ... */
1688 1455
1689 #define HACKEQ_UNSAFE(obj1, obj2) \ 1456 #define HACKEQ_UNSAFE(obj1, obj2) \
1690 (EQ (obj1, obj2) || (!POINTER_TYPE_P (XTYPE (obj1)) \ 1457 (EQ (obj1, obj2) || (!POINTER_TYPE_P (XTYPE (obj1)) \
1691 && !POINTER_TYPE_P (XTYPE (obj2)) \ 1458 && !POINTER_TYPE_P (XTYPE (obj2)) \
1711 1478
1712 #include "lrecord.h" 1479 #include "lrecord.h"
1713 1480
1714 BEGIN_C_DECLS 1481 BEGIN_C_DECLS
1715 1482
1483 /* ------------------------ dynamic arrays ------------------- */
1484
1716 #ifdef NEW_GC 1485 #ifdef NEW_GC
1717 /* ------------------------ dynamic arrays ------------------- */
1718
1719 #ifdef ERROR_CHECK_STRUCTURES 1486 #ifdef ERROR_CHECK_STRUCTURES
1720 #define Dynarr_declare(type) \ 1487 #define Dynarr_declare(type) \
1721 struct lrecord_header header; \ 1488 struct lrecord_header header; \
1722 type *base; \ 1489 type *base; \
1723 const struct lrecord_implementation *lisp_imp; \ 1490 const struct lrecord_implementation *lisp_imp; \
1734 int elsize; \ 1501 int elsize; \
1735 int cur; \ 1502 int cur; \
1736 int largest; \ 1503 int largest; \
1737 int max 1504 int max
1738 #endif /* ERROR_CHECK_STRUCTURES */ 1505 #endif /* ERROR_CHECK_STRUCTURES */
1506 #else /* not NEW_GC */
1507 #ifdef ERROR_CHECK_STRUCTURES
1508 #define Dynarr_declare(type) \
1509 struct lrecord_header header; \
1510 type *base; \
1511 int locked; \
1512 int elsize; \
1513 int cur; \
1514 int largest; \
1515 int max
1516 #else
1517 #define Dynarr_declare(type) \
1518 struct lrecord_header header; \
1519 type *base; \
1520 int elsize; \
1521 int cur; \
1522 int largest; \
1523 int max
1524 #endif /* ERROR_CHECK_STRUCTURES */
1525 #endif /* not NEW_GC */
1739 1526
1740 typedef struct dynarr 1527 typedef struct dynarr
1741 { 1528 {
1742 Dynarr_declare (void); 1529 Dynarr_declare (void);
1743 } Dynarr; 1530 } Dynarr;
1746 MODULE_API void Dynarr_resize (void *dy, Elemcount size); 1533 MODULE_API void Dynarr_resize (void *dy, Elemcount size);
1747 MODULE_API void Dynarr_insert_many (void *d, const void *el, int len, int start); 1534 MODULE_API void Dynarr_insert_many (void *d, const void *el, int len, int start);
1748 MODULE_API void Dynarr_delete_many (void *d, int start, int len); 1535 MODULE_API void Dynarr_delete_many (void *d, int start, int len);
1749 MODULE_API void Dynarr_free (void *d); 1536 MODULE_API void Dynarr_free (void *d);
1750 1537
1538 #ifdef NEW_GC
1751 MODULE_API void *Dynarr_lisp_newf (int elsize, 1539 MODULE_API void *Dynarr_lisp_newf (int elsize,
1752 const struct lrecord_implementation 1540 const struct lrecord_implementation
1753 *dynarr_imp, 1541 *dynarr_imp,
1754 const struct lrecord_implementation *imp); 1542 const struct lrecord_implementation *imp);
1755 1543
1756 #define Dynarr_lisp_new(type, dynarr_imp, imp) \ 1544 #define Dynarr_lisp_new(type, dynarr_imp, imp) \
1757 ((type##_dynarr *) Dynarr_lisp_newf (sizeof (type), dynarr_imp, imp)) 1545 ((type##_dynarr *) Dynarr_lisp_newf (sizeof (type), dynarr_imp, imp))
1758 #define Dynarr_lisp_new2(dynarr_type, type, dynarr_imp, imp) \ 1546 #define Dynarr_lisp_new2(dynarr_type, type, dynarr_imp, imp) \
1759 ((dynarr_type *) Dynarr_lisp_newf (sizeof (type)), dynarr_imp, imp) 1547 ((dynarr_type *) Dynarr_lisp_newf (sizeof (type)), dynarr_imp, imp)
1548 #endif /* NEW_GC */
1760 #define Dynarr_new(type) ((type##_dynarr *) Dynarr_newf (sizeof (type))) 1549 #define Dynarr_new(type) ((type##_dynarr *) Dynarr_newf (sizeof (type)))
1761 #define Dynarr_new2(dynarr_type, type) \ 1550 #define Dynarr_new2(dynarr_type, type) \
1762 ((dynarr_type *) Dynarr_newf (sizeof (type))) 1551 ((dynarr_type *) Dynarr_newf (sizeof (type)))
1763 #define Dynarr_at(d, pos) ((d)->base[pos]) 1552 #define Dynarr_at(d, pos) ((d)->base[pos])
1764 #define Dynarr_atp(d, pos) (&Dynarr_at (d, pos)) 1553 #define Dynarr_atp(d, pos) (&Dynarr_at (d, pos))
1818 LISP_STRING_TO_SIZED_EXTERNAL (dyna_ls_s, dyna_ls_eb, \ 1607 LISP_STRING_TO_SIZED_EXTERNAL (dyna_ls_s, dyna_ls_eb, \
1819 dyna_ls_bc, dyna_ls_cs); \ 1608 dyna_ls_bc, dyna_ls_cs); \
1820 Dynarr_add_many (d, dyna_ls_eb, dyna_ls_bc); \ 1609 Dynarr_add_many (d, dyna_ls_eb, dyna_ls_bc); \
1821 } while (0) 1610 } while (0)
1822 1611
1823 #if 1 1612 #ifdef NEW_GC
1824 #define Dynarr_add(d, el) \ 1613 #define Dynarr_add(d, el) \
1825 do { \ 1614 do { \
1826 if (Dynarr_verify_mod (d)->cur >= (d)->max) \ 1615 if (Dynarr_verify_mod (d)->cur >= (d)->max) \
1827 Dynarr_resize ((d), (d)->cur+1); \ 1616 Dynarr_resize ((d), (d)->cur+1); \
1828 ((d)->base)[(d)->cur] = (el); \ 1617 ((d)->base)[(d)->cur] = (el); \
1834 \ 1623 \
1835 (d)->cur++; \ 1624 (d)->cur++; \
1836 if ((d)->cur > (d)->largest) \ 1625 if ((d)->cur > (d)->largest) \
1837 (d)->largest = (d)->cur; \ 1626 (d)->largest = (d)->cur; \
1838 } while (0) 1627 } while (0)
1839 #else 1628 #else /* not NEW_GC */
1840 #define Dynarr_add(d, el) ( \ 1629 #define Dynarr_add(d, el) ( \
1841 Dynarr_verify_mod (d)->cur >= (d)->max ? Dynarr_resize ((d), (d)->cur+1) : \ 1630 Dynarr_verify_mod (d)->cur >= (d)->max ? Dynarr_resize ((d), (d)->cur+1) : \
1842 (void) 0, \ 1631 (void) 0, \
1843 ((d)->base)[(d)->cur++] = (el), \ 1632 ((d)->base)[(d)->cur++] = (el), \
1844 (d)->cur > (d)->largest ? (d)->largest = (d)->cur : (int) 0) 1633 (d)->cur > (d)->largest ? (d)->largest = (d)->cur : (int) 0)
1845 #endif 1634 #endif /* not NEW_GC */
1846 1635
1847 1636
1848 /* The following defines will get you into real trouble if you aren't 1637 /* The following defines will get you into real trouble if you aren't
1849 careful. But they can save a lot of execution time when used wisely. */ 1638 careful. But they can save a lot of execution time when used wisely. */
1850 #define Dynarr_increment(d) (Dynarr_verify_mod (d)->cur++) 1639 #define Dynarr_increment(d) (Dynarr_verify_mod (d)->cur++)
1949 typedef struct 1738 typedef struct
1950 { 1739 {
1951 Dynarr_declare (struct face_cachel); 1740 Dynarr_declare (struct face_cachel);
1952 } face_cachel_dynarr; 1741 } face_cachel_dynarr;
1953 1742
1743 #ifdef NEW_GC
1954 DECLARE_LRECORD (face_cachel_dynarr, face_cachel_dynarr); 1744 DECLARE_LRECORD (face_cachel_dynarr, face_cachel_dynarr);
1955 #define XFACE_CACHEL_DYNARR(x) \ 1745 #define XFACE_CACHEL_DYNARR(x) \
1956 XRECORD (x, face_cachel_dynarr, face_cachel_dynarr) 1746 XRECORD (x, face_cachel_dynarr, face_cachel_dynarr)
1957 #define wrap_face_cachel_dynarr(p) wrap_record (p, face_cachel_dynarr) 1747 #define wrap_face_cachel_dynarr(p) wrap_record (p, face_cachel_dynarr)
1958 #define FACE_CACHEL_DYNARRP(x) RECORDP (x, face_cachel_dynarr) 1748 #define FACE_CACHEL_DYNARRP(x) RECORDP (x, face_cachel_dynarr)
1959 #define CHECK_FACE_CACHEL_DYNARR(x) CHECK_RECORD (x, face_cachel_dynarr) 1749 #define CHECK_FACE_CACHEL_DYNARR(x) CHECK_RECORD (x, face_cachel_dynarr)
1960 #define CONCHECK_FACE_CACHEL_DYNARR(x) CONCHECK_RECORD (x, face_cachel_dynarr) 1750 #define CONCHECK_FACE_CACHEL_DYNARR(x) CONCHECK_RECORD (x, face_cachel_dynarr)
1751 #endif /* NEW_GC */
1961 1752
1962 typedef struct 1753 typedef struct
1963 { 1754 {
1964 Dynarr_declare (struct glyph_cachel); 1755 Dynarr_declare (struct glyph_cachel);
1965 } glyph_cachel_dynarr; 1756 } glyph_cachel_dynarr;
1966 1757
1758 #ifdef NEW_GC
1967 DECLARE_LRECORD (glyph_cachel_dynarr, glyph_cachel_dynarr); 1759 DECLARE_LRECORD (glyph_cachel_dynarr, glyph_cachel_dynarr);
1968 #define XGLYPH_CACHEL_DYNARR(x) \ 1760 #define XGLYPH_CACHEL_DYNARR(x) \
1969 XRECORD (x, glyph_cachel_dynarr, glyph_cachel_dynarr) 1761 XRECORD (x, glyph_cachel_dynarr, glyph_cachel_dynarr)
1970 #define wrap_glyph_cachel_dynarr(p) wrap_record (p, glyph_cachel_dynarr) 1762 #define wrap_glyph_cachel_dynarr(p) wrap_record (p, glyph_cachel_dynarr)
1971 #define GLYPH_CACHEL_DYNARRP(x) RECORDP (x, glyph_cachel_dynarr) 1763 #define GLYPH_CACHEL_DYNARRP(x) RECORDP (x, glyph_cachel_dynarr)
1972 #define CHECK_GLYPH_CACHEL_DYNARR(x) CHECK_RECORD (x, glyph_cachel_dynarr) 1764 #define CHECK_GLYPH_CACHEL_DYNARR(x) CHECK_RECORD (x, glyph_cachel_dynarr)
1973 #define CONCHECK_GLYPH_CACHEL_DYNARR(x) \ 1765 #define CONCHECK_GLYPH_CACHEL_DYNARR(x) \
1974 CONCHECK_RECORD (x, glyph_cachel_dynarr) 1766 CONCHECK_RECORD (x, glyph_cachel_dynarr)
1767 #endif /* NEW_GC */
1975 1768
1976 typedef struct 1769 typedef struct
1977 { 1770 {
1978 Dynarr_declare (struct console_type_entry); 1771 Dynarr_declare (struct console_type_entry);
1979 } console_type_entry_dynarr; 1772 } console_type_entry_dynarr;
1987 1780
1988 typedef struct 1781 typedef struct
1989 { 1782 {
1990 Dynarr_declare (Lisp_Object *); 1783 Dynarr_declare (Lisp_Object *);
1991 } Lisp_Object_ptr_dynarr; 1784 } Lisp_Object_ptr_dynarr;
1992 #endif /* NEW_GC */
1993 1785
1994 /*------------------------------ unbound -------------------------------*/ 1786 /*------------------------------ unbound -------------------------------*/
1995 1787
1996 /* Qunbound is a special Lisp_Object (actually of type 1788 /* Qunbound is a special Lisp_Object (actually of type
1997 symbol-value-forward), that can never be visible to 1789 symbol-value-forward), that can never be visible to