| 1373 | | |
|---|
| 1374 | | |
|---|
| 1375 | | |
|---|
| 1376 | | /* |
|---|
| 1377 | | * The "quark" package |
|---|
| 1378 | | * |
|---|
| 1379 | | * This package is used to reduce the memory usage of object inscriptions. |
|---|
| 1380 | | * |
|---|
| 1381 | | * We use dynamic string allocation because otherwise it is necessary to |
|---|
| 1382 | | * pre-guess the amount of quark activity. We limit the total number of |
|---|
| 1383 | | * quarks, but this is much easier to "expand" as needed. XXX XXX XXX |
|---|
| 1384 | | * |
|---|
| 1385 | | * Two objects with the same inscription will have the same "quark" index. |
|---|
| 1386 | | * |
|---|
| 1387 | | * Some code uses "zero" to indicate the non-existance of a quark. |
|---|
| 1388 | | * |
|---|
| 1389 | | * Note that "quark zero" is NULL and should never be "dereferenced". |
|---|
| 1390 | | * |
|---|
| 1391 | | * ToDo: Add reference counting for quarks, so that unused quarks can |
|---|
| 1392 | | * be overwritten. |
|---|
| 1393 | | * |
|---|
| 1394 | | * ToDo: Automatically resize the array if necessary. |
|---|
| 1395 | | */ |
|---|
| 1396 | | |
|---|
| 1397 | | |
|---|
| 1398 | | /* |
|---|
| 1399 | | * The number of quarks (first quark is NULL) |
|---|
| 1400 | | */ |
|---|
| 1401 | | static s16b quark__num = 1; |
|---|
| 1402 | | |
|---|
| 1403 | | |
|---|
| 1404 | | /* |
|---|
| 1405 | | * The array[QUARK_MAX] of pointers to the quarks |
|---|
| 1406 | | */ |
|---|
| 1407 | | static char **quark__str; |
|---|
| 1408 | | |
|---|
| 1409 | | |
|---|
| 1410 | | /* |
|---|
| 1411 | | * Add a new "quark" to the set of quarks. |
|---|
| 1412 | | */ |
|---|
| 1413 | | s16b quark_add(cptr str) |
|---|
| 1414 | | { |
|---|
| 1415 | | int i; |
|---|
| 1416 | | |
|---|
| 1417 | | /* Look for an existing quark */ |
|---|
| 1418 | | for (i = 1; i < quark__num; i++) |
|---|
| 1419 | | { |
|---|
| 1420 | | /* Check for equality */ |
|---|
| 1421 | | if (streq(quark__str[i], str)) return (i); |
|---|
| 1422 | | } |
|---|
| 1423 | | |
|---|
| 1424 | | /* Hack -- Require room XXX XXX XXX */ |
|---|
| 1425 | | if (quark__num == QUARK_MAX) return (0); |
|---|
| 1426 | | |
|---|
| 1427 | | /* New quark */ |
|---|
| 1428 | | i = quark__num++; |
|---|
| 1429 | | |
|---|
| 1430 | | /* Add a new quark */ |
|---|
| 1431 | | quark__str[i] = string_make(str); |
|---|
| 1432 | | |
|---|
| 1433 | | /* Return the index */ |
|---|
| 1434 | | return (i); |
|---|
| 1435 | | } |
|---|
| 1436 | | |
|---|
| 1437 | | |
|---|
| 1438 | | /* |
|---|
| 1439 | | * This function looks up a quark |
|---|
| 1440 | | */ |
|---|
| 1441 | | cptr quark_str(s16b i) |
|---|
| 1442 | | { |
|---|
| 1443 | | cptr q; |
|---|
| 1444 | | |
|---|
| 1445 | | /* Verify */ |
|---|
| 1446 | | if ((i < 0) || (i >= quark__num)) i = 0; |
|---|
| 1447 | | |
|---|
| 1448 | | /* Get the quark */ |
|---|
| 1449 | | q = quark__str[i]; |
|---|
| 1450 | | |
|---|
| 1451 | | /* Return the quark */ |
|---|
| 1452 | | return (q); |
|---|
| 1453 | | } |
|---|
| 1454 | | |
|---|
| 1455 | | |
|---|
| 1456 | | /* |
|---|
| 1457 | | * Initialize the "quark" package |
|---|
| 1458 | | */ |
|---|
| 1459 | | errr quarks_init(void) |
|---|
| 1460 | | { |
|---|
| 1461 | | /* Quark variables */ |
|---|
| 1462 | | quark__str = C_ZNEW(QUARK_MAX, const char *); |
|---|
| 1463 | | |
|---|
| 1464 | | /* Success */ |
|---|
| 1465 | | return (0); |
|---|
| 1466 | | } |
|---|
| 1467 | | |
|---|
| 1468 | | |
|---|
| 1469 | | /* |
|---|
| 1470 | | * Free the "quark" package |
|---|
| 1471 | | */ |
|---|
| 1472 | | errr quarks_free(void) |
|---|
| 1473 | | { |
|---|
| 1474 | | int i; |
|---|
| 1475 | | |
|---|
| 1476 | | /* Free the "quarks" */ |
|---|
| 1477 | | for (i = 1; i < quark__num; i++) |
|---|
| 1478 | | string_free(quark__str[i]); |
|---|
| 1479 | | |
|---|
| 1480 | | /* Free the list of "quarks" */ |
|---|
| 1481 | | FREE(quark__str); |
|---|
| 1482 | | |
|---|
| 1483 | | /* Success */ |
|---|
| 1484 | | return (0); |
|---|
| 1485 | | } |
|---|