delete.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * delete.c - Object deletion
  3. *
  4. * Written 2009, 2010, 2012 by Werner Almesberger
  5. * Copyright 2009, 2010, 2012 by Werner Almesberger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <stdlib.h>
  13. #include <assert.h>
  14. #include "util.h"
  15. #include "error.h"
  16. #include "expr.h"
  17. #include "obj.h"
  18. #include "delete.h"
  19. static struct deletion {
  20. enum del_type {
  21. dt_vec,
  22. dt_obj,
  23. dt_frame,
  24. dt_table,
  25. dt_row,
  26. dt_column,
  27. dt_loop,
  28. } type;
  29. union {
  30. struct {
  31. struct frame *ref;
  32. struct frame *prev;
  33. } frame;
  34. struct {
  35. struct vec *ref;
  36. struct vec *prev;
  37. } vec;
  38. struct {
  39. struct obj *ref;
  40. struct obj *prev;
  41. } obj;
  42. struct {
  43. struct table *ref;
  44. struct table *prev;
  45. } table;
  46. struct {
  47. struct row *ref;
  48. struct row *prev;
  49. } row;
  50. struct column {
  51. struct var *var;
  52. struct value *values;
  53. struct table *table;
  54. int n;
  55. } col;
  56. struct {
  57. struct loop *ref;
  58. struct loop *prev;
  59. } loop;
  60. } u;
  61. int group;
  62. struct deletion *next;
  63. } *deletions = NULL;
  64. static int groups = 0;
  65. static void do_delete_vec(struct vec *vec);
  66. static void do_delete_obj(struct obj *obj);
  67. /* ----- helper functions -------------------------------------------------- */
  68. static struct deletion *new_deletion(enum del_type type)
  69. {
  70. struct deletion *del;
  71. del = alloc_type(struct deletion);
  72. del->type = type;
  73. del->group = groups;
  74. del->next = deletions;
  75. deletions = del;
  76. return del;
  77. }
  78. static void reset_active_ref(struct frame *ref)
  79. {
  80. const struct frame *frame;
  81. struct obj *obj = NULL;
  82. for (frame = frames; frame; frame = frame->next)
  83. for (obj = frame->objs; obj; obj = obj->next)
  84. if (obj->type == ot_frame && obj->u.frame.ref == ref)
  85. break;
  86. ref->active_ref = obj;
  87. }
  88. /* ----- vectors ----------------------------------------------------------- */
  89. static void destroy_vec(struct vec *vec)
  90. {
  91. free_expr(vec->x);
  92. free_expr(vec->y);
  93. free(vec);
  94. }
  95. static void delete_vecs_by_ref(struct vec *vecs, const struct vec *ref)
  96. {
  97. while (vecs) {
  98. if (vecs->base == ref)
  99. do_delete_vec(vecs);
  100. vecs = vecs->next;
  101. }
  102. }
  103. static int obj_has_ref(const struct obj *obj, const struct vec *ref)
  104. {
  105. if (obj->base == ref)
  106. return 1;
  107. switch (obj->type) {
  108. case ot_frame:
  109. return 0;
  110. case ot_line:
  111. return obj->u.line.other == ref;
  112. case ot_rect:
  113. return obj->u.rect.other == ref;
  114. case ot_pad:
  115. return obj->u.pad.other == ref;
  116. case ot_hole:
  117. return obj->u.hole.other == ref;
  118. case ot_arc:
  119. return obj->u.arc.start == ref || obj->u.arc.end == ref;
  120. case ot_meas:
  121. return obj->u.meas.high == ref;
  122. case ot_iprint:
  123. return 0;
  124. default:
  125. abort();
  126. }
  127. }
  128. static void delete_objs_by_ref(struct obj **objs, const struct vec *ref)
  129. {
  130. struct obj *obj;
  131. for (obj = *objs; obj; obj = obj->next)
  132. if (obj_has_ref(obj, ref))
  133. do_delete_obj(obj);
  134. }
  135. static void do_delete_vec(struct vec *vec)
  136. {
  137. struct vec *walk, *prev;
  138. struct deletion *del;
  139. prev = NULL;
  140. for (walk = vec->frame->vecs; walk != vec; walk = walk->next)
  141. prev = walk;
  142. if (prev)
  143. prev->next = vec->next;
  144. else
  145. vec->frame->vecs = vec->next;
  146. del = new_deletion(dt_vec);
  147. del->u.vec.ref = vec;
  148. del->u.vec.prev = prev;
  149. delete_vecs_by_ref(vec->frame->vecs, vec);
  150. delete_objs_by_ref(&vec->frame->objs, vec);
  151. /*
  152. * Catch measurements. During final cleanup, we may operate on an empty
  153. * list of frames, hence the test.
  154. */
  155. if (frames)
  156. delete_objs_by_ref(&frames->objs, vec);
  157. }
  158. void delete_vec(struct vec *vec)
  159. {
  160. groups++;
  161. do_delete_vec(vec);
  162. }
  163. static void undelete_vec(struct vec *vec, struct vec *prev)
  164. {
  165. if (prev) {
  166. assert(vec->next == prev->next);
  167. prev->next = vec;
  168. } else {
  169. assert(vec->next == vec->frame->vecs);
  170. vec->frame->vecs = vec;
  171. }
  172. }
  173. /* ----- objects ----------------------------------------------------------- */
  174. static void destroy_obj(struct obj *obj)
  175. {
  176. switch (obj->type) {
  177. case ot_frame:
  178. if (obj->u.frame.ref->active_ref == obj)
  179. reset_active_ref(obj->u.frame.ref);
  180. break;
  181. case ot_pad:
  182. free(obj->u.pad.name);
  183. break;
  184. case ot_hole:
  185. break;
  186. case ot_line:
  187. if (obj->u.line.width)
  188. free_expr(obj->u.line.width);
  189. break;
  190. case ot_rect:
  191. if (obj->u.rect.width)
  192. free_expr(obj->u.rect.width);
  193. break;
  194. case ot_arc:
  195. if (obj->u.arc.width)
  196. free_expr(obj->u.arc.width);
  197. break;
  198. case ot_meas:
  199. if (obj->u.meas.label)
  200. free(obj->u.meas.label);
  201. if (obj->u.meas.offset)
  202. free_expr(obj->u.meas.offset);
  203. break;
  204. case ot_iprint:
  205. free_expr(obj->u.iprint.expr);
  206. break;
  207. default:
  208. abort();
  209. }
  210. free(obj);
  211. }
  212. static void do_delete_obj(struct obj *obj)
  213. {
  214. struct obj *walk, *prev;
  215. struct deletion *del;
  216. prev = NULL;
  217. for (walk = obj->frame->objs; walk != obj; walk = walk->next)
  218. prev = walk;
  219. if (prev)
  220. prev->next = obj->next;
  221. else
  222. obj->frame->objs = obj->next;
  223. del = new_deletion(dt_obj);
  224. del->u.obj.ref = obj;
  225. del->u.obj.prev = prev;
  226. if (obj->type == ot_frame && obj->u.frame.ref->active_ref == obj)
  227. reset_active_ref(obj->u.frame.ref);
  228. }
  229. void delete_obj(struct obj *obj)
  230. {
  231. groups++;
  232. do_delete_obj(obj);
  233. }
  234. static void undelete_obj(struct obj *obj, struct obj *prev)
  235. {
  236. if (prev) {
  237. assert(obj->next == prev->next);
  238. prev->next = obj;
  239. } else {
  240. assert(obj->next == obj->frame->objs);
  241. obj->frame->objs = obj;
  242. }
  243. }
  244. /* ----- rows -------------------------------------------------------------- */
  245. static void destroy_row(struct row *row)
  246. {
  247. struct value *next_value;
  248. while (row->values) {
  249. next_value = row->values->next;
  250. free_expr(row->values->expr);
  251. free(row->values);
  252. row->values = next_value;
  253. }
  254. free(row);
  255. }
  256. void delete_row(struct row *row)
  257. {
  258. struct deletion *del;
  259. struct row *walk, *prev;
  260. groups++;
  261. prev = NULL;
  262. for (walk = row->table->rows; walk != row; walk = walk->next)
  263. prev = walk;
  264. if (prev)
  265. prev->next = row->next;
  266. else
  267. row->table->rows = row->next;
  268. del = new_deletion(dt_row);
  269. del->u.row.ref = row;
  270. del->u.row.prev = prev;
  271. }
  272. static void undelete_row(struct row *row, struct row *prev)
  273. {
  274. if (prev) {
  275. assert(row->next == prev->next);
  276. prev->next = row;
  277. } else {
  278. assert(row->next == row->table->rows);
  279. row->table->rows = row;
  280. }
  281. }
  282. /* ----- columns ----------------------------------------------------------- */
  283. void delete_column(struct table *table, int n)
  284. {
  285. struct deletion *del;
  286. struct column *col;
  287. struct var **var;
  288. struct row *row;
  289. struct value **next, **value;
  290. int i;
  291. groups++;
  292. del = new_deletion(dt_column);
  293. col = &del->u.col;
  294. col->table = table;
  295. col->n = n;
  296. var = &table->vars;
  297. for (i = 0; i != n; i++)
  298. var = &(*var)->next;
  299. col->var = *var;
  300. *var = (*var)->next;
  301. next = &col->values;
  302. for (row = table->rows; row; row = row->next) {
  303. value = &row->values;
  304. for (i = 0; i != n; i++)
  305. value = &(*value)->next;
  306. *next = *value;
  307. *value = (*value)->next;
  308. next = &(*next)->next;
  309. }
  310. *next = NULL;
  311. }
  312. static void undelete_column(const struct column *col)
  313. {
  314. struct var **var;
  315. struct row *row;
  316. struct value **anchor, *value, *next;
  317. int i;
  318. var = &col->table->vars;
  319. for (i = 0; i != col->n; i++)
  320. var = &(*var)->next;
  321. col->var->next = *var;
  322. *var = col->var;
  323. value = col->values;
  324. for (row = col->table->rows; row; row = row->next) {
  325. anchor = &row->values;
  326. for (i = 0; i != col->n; i++)
  327. anchor = &(*anchor)->next;
  328. next = value->next;
  329. value->next = *anchor;
  330. *anchor = value;
  331. value = next;
  332. }
  333. }
  334. /* ----- tables ------------------------------------------------------------ */
  335. static void destroy_table(struct table *table)
  336. {
  337. struct var *next_var;
  338. while (table->vars) {
  339. next_var = table->vars->next;
  340. free(table->vars);
  341. table->vars = next_var;
  342. }
  343. while (table->rows) {
  344. delete_row(table->rows);
  345. destroy();
  346. }
  347. free(table);
  348. }
  349. void delete_table(struct table *table)
  350. {
  351. struct frame *frame = table->vars->frame;
  352. struct deletion *del;
  353. struct table *walk, *prev;
  354. groups++;
  355. prev = NULL;
  356. for (walk = frame->tables; walk != table; walk = walk->next)
  357. prev = walk;
  358. if (prev)
  359. prev->next = table->next;
  360. else
  361. frame->tables = table->next;
  362. del = new_deletion(dt_table);
  363. del->u.table.ref = table;
  364. del->u.table.prev = prev;
  365. }
  366. static void undelete_table(struct table *table, struct table *prev)
  367. {
  368. struct frame *frame = table->vars->frame;
  369. if (prev) {
  370. assert(table->next == prev->next);
  371. prev->next = table;
  372. } else {
  373. assert(table->next == frame->tables);
  374. frame->tables = table;
  375. }
  376. }
  377. /* ----- loops ------------------------------------------------------------- */
  378. static void destroy_loop(struct loop *loop)
  379. {
  380. free_expr(loop->from.expr);
  381. free_expr(loop->to.expr);
  382. free(loop);
  383. }
  384. void delete_loop(struct loop *loop)
  385. {
  386. struct frame *frame = loop->var.frame;
  387. struct deletion *del;
  388. struct loop *walk, *prev;
  389. groups++;
  390. prev = NULL;
  391. for (walk = frame->loops; walk != loop; walk = walk->next)
  392. prev = walk;
  393. if (prev)
  394. prev->next = loop->next;
  395. else
  396. frame->loops = loop->next;
  397. del = new_deletion(dt_loop);
  398. del->u.loop.ref = loop;
  399. del->u.loop.prev = prev;
  400. }
  401. static void undelete_loop(struct loop *loop, struct loop *prev)
  402. {
  403. struct frame *frame = loop->var.frame;
  404. if (prev) {
  405. assert(loop->next == prev->next);
  406. prev->next = loop;
  407. } else {
  408. assert(loop->next == frame->loops);
  409. frame->loops = loop;
  410. }
  411. }
  412. /* ----- frames ------------------------------------------------------------ */
  413. static void destroy_frame(struct frame *frame)
  414. {
  415. while (frame->tables) {
  416. delete_table(frame->tables);
  417. destroy();
  418. }
  419. while (frame->loops) {
  420. delete_loop(frame->loops);
  421. destroy();
  422. }
  423. while (frame->vecs) {
  424. delete_vec(frame->vecs);
  425. destroy();
  426. }
  427. while (frame->objs) {
  428. delete_obj(frame->objs);
  429. destroy();
  430. }
  431. free(frame);
  432. }
  433. static int qual_ref(const struct frame_qual *qual, const struct frame *ref)
  434. {
  435. while (qual) {
  436. if (qual->frame == ref)
  437. return 1;
  438. qual = qual->next;
  439. }
  440. return 0;
  441. }
  442. static void delete_references(const struct frame *ref)
  443. {
  444. struct frame *frame;
  445. struct obj *obj;
  446. for (frame = frames; frame; frame = frame->next)
  447. for (obj = frame->objs; obj; obj = obj->next)
  448. switch (obj->type) {
  449. case ot_frame:
  450. if (obj->u.frame.ref == ref)
  451. do_delete_obj(obj);
  452. break;
  453. case ot_meas:
  454. if (obj->base->frame == ref ||
  455. obj->u.meas.high->frame == ref ||
  456. qual_ref(obj->u.meas.low_qual, ref) ||
  457. qual_ref(obj->u.meas.high_qual, ref))
  458. do_delete_obj(obj);
  459. break;
  460. default:
  461. break;
  462. }
  463. for (obj = ref->objs; obj; obj = obj->next)
  464. if (obj->type == ot_frame)
  465. if (obj->u.frame.ref->active_ref == obj)
  466. reset_active_ref(obj->u.frame.ref);
  467. }
  468. void delete_frame(struct frame *frame)
  469. {
  470. struct deletion *del;
  471. struct frame *walk;
  472. groups++;
  473. del = new_deletion(dt_frame);
  474. del->u.frame.ref = frame;
  475. del->u.frame.prev = NULL;
  476. for (walk = frames; walk != frame; walk = walk->next)
  477. del->u.frame.prev = walk;
  478. if (del->u.frame.prev)
  479. del->u.frame.prev->next = frame->next;
  480. else
  481. frames = frame->next; /* hmm, deleting the root frame ? */
  482. delete_references(frame);
  483. }
  484. static void undelete_frame(struct frame *frame, struct frame *prev)
  485. {
  486. if (prev) {
  487. assert(frame->next == prev->next);
  488. prev->next = frame;
  489. } else {
  490. assert(frame->next == frames);
  491. frames = frame;
  492. }
  493. }
  494. /* ----- destroy/undelete interface ---------------------------------------- */
  495. static void destroy_one(void)
  496. {
  497. struct deletion *del;
  498. del = deletions;
  499. switch (del->type) {
  500. case dt_vec:
  501. destroy_vec(del->u.vec.ref);
  502. break;
  503. case dt_obj:
  504. destroy_obj(del->u.obj.ref);
  505. break;
  506. case dt_frame:
  507. destroy_frame(del->u.frame.ref);
  508. break;
  509. case dt_loop:
  510. destroy_loop(del->u.loop.ref);
  511. break;
  512. case dt_table:
  513. destroy_table(del->u.table.ref);
  514. break;
  515. case dt_row:
  516. destroy_row(del->u.row.ref);
  517. break;
  518. case dt_column:
  519. abort(); /* @@@ later */
  520. break;
  521. default:
  522. abort();
  523. }
  524. deletions = del->next;
  525. free(del);
  526. }
  527. void destroy(void)
  528. {
  529. int group;
  530. assert(deletions);
  531. group = deletions->group;
  532. while (deletions && deletions->group == group)
  533. destroy_one();
  534. }
  535. static int undelete_one(void)
  536. {
  537. struct deletion *del;
  538. if (!deletions)
  539. return 0;
  540. del = deletions;
  541. switch (del->type) {
  542. case dt_vec:
  543. undelete_vec(del->u.vec.ref, del->u.vec.prev);
  544. break;
  545. case dt_obj:
  546. undelete_obj(del->u.obj.ref, del->u.obj.prev);
  547. break;
  548. case dt_frame:
  549. undelete_frame(del->u.frame.ref, del->u.frame.prev);
  550. break;
  551. case dt_loop:
  552. undelete_loop(del->u.loop.ref, del->u.loop.prev);
  553. break;
  554. case dt_table:
  555. undelete_table(del->u.table.ref, del->u.table.prev);
  556. break;
  557. case dt_row:
  558. undelete_row(del->u.row.ref, del->u.row.prev);
  559. break;
  560. case dt_column:
  561. undelete_column(&del->u.col);
  562. break;
  563. default:
  564. abort();
  565. }
  566. deletions = del->next;
  567. free(del);
  568. return 1;
  569. }
  570. int undelete(void)
  571. {
  572. int group;
  573. if (!deletions)
  574. return 0;
  575. group = deletions->group;
  576. while (deletions && deletions->group == group)
  577. undelete_one();
  578. return 1;
  579. }
  580. void purge(void)
  581. {
  582. while (deletions)
  583. destroy();
  584. }