comparison src/alloc.c @ 72156:b4ec5a95c687

(valid_pointer_p): New function (from valid_lisp_object_p). (valid_lisp_object_p): Use it to check for valid SUBRP obj.
author Kim F. Storm <storm@cua.dk>
date Fri, 28 Jul 2006 11:12:23 +0000
parents fe7f8d2385f8
children b0a67cf52eb6
comparison
equal deleted inserted replaced
72155:64892d9c016e 72156:b4ec5a95c687
4604 } 4604 }
4605 4605
4606 #endif /* GC_MARK_STACK != 0 */ 4606 #endif /* GC_MARK_STACK != 0 */
4607 4607
4608 4608
4609 /* Determine whether it is safe to access memory at address P. */
4610 int valid_pointer_p (p)
4611 void *p;
4612 {
4613 int fd;
4614
4615 /* Obviously, we cannot just access it (we would SEGV trying), so we
4616 trick the o/s to tell us whether p is a valid pointer.
4617 Unfortunately, we cannot use NULL_DEVICE here, as emacs_write may
4618 not validate p in that case. */
4619
4620 if ((fd = emacs_open ("__Valid__Lisp__Object__", O_CREAT | O_WRONLY | O_TRUNC, 0666)) >= 0)
4621 {
4622 int valid = (emacs_write (fd, (char *)p, 16) == 16);
4623 emacs_close (fd);
4624 unlink ("__Valid__Lisp__Object__");
4625 return valid;
4626 }
4627
4628 return -1;
4629 }
4609 4630
4610 /* Return 1 if OBJ is a valid lisp object. 4631 /* Return 1 if OBJ is a valid lisp object.
4611 Return 0 if OBJ is NOT a valid lisp object. 4632 Return 0 if OBJ is NOT a valid lisp object.
4612 Return -1 if we cannot validate OBJ. 4633 Return -1 if we cannot validate OBJ.
4613 This function can be quite slow, 4634 This function can be quite slow,
4616 int 4637 int
4617 valid_lisp_object_p (obj) 4638 valid_lisp_object_p (obj)
4618 Lisp_Object obj; 4639 Lisp_Object obj;
4619 { 4640 {
4620 void *p; 4641 void *p;
4621 #if !GC_MARK_STACK 4642 #if GC_MARK_STACK
4622 int fd;
4623 #else
4624 struct mem_node *m; 4643 struct mem_node *m;
4625 #endif 4644 #endif
4626 4645
4627 if (INTEGERP (obj)) 4646 if (INTEGERP (obj))
4628 return 1; 4647 return 1;
4630 p = (void *) XPNTR (obj); 4649 p = (void *) XPNTR (obj);
4631 if (PURE_POINTER_P (p)) 4650 if (PURE_POINTER_P (p))
4632 return 1; 4651 return 1;
4633 4652
4634 #if !GC_MARK_STACK 4653 #if !GC_MARK_STACK
4635 /* We need to determine whether it is safe to access memory at 4654 return valid_pointer_p (p);
4636 address P. Obviously, we cannot just access it (we would SEGV
4637 trying), so we trick the o/s to tell us whether p is a valid
4638 pointer. Unfortunately, we cannot use NULL_DEVICE here, as
4639 emacs_write may not validate p in that case. */
4640 if ((fd = emacs_open ("__Valid__Lisp__Object__", O_CREAT | O_WRONLY | O_TRUNC, 0666)) >= 0)
4641 {
4642 int valid = (emacs_write (fd, (char *)p, 16) == 16);
4643 emacs_close (fd);
4644 unlink ("__Valid__Lisp__Object__");
4645 return valid;
4646 }
4647
4648 return -1;
4649 #else 4655 #else
4650 4656
4651 m = mem_find (p); 4657 m = mem_find (p);
4652 4658
4653 if (m == MEM_NIL) 4659 if (m == MEM_NIL)
4654 return 0; 4660 {
4661 int valid = valid_pointer_p (p);
4662 if (valid <= 0)
4663 return valid;
4664
4665 if (SUBRP (obj))
4666 return 1;
4667
4668 return 0;
4669 }
4655 4670
4656 switch (m->type) 4671 switch (m->type)
4657 { 4672 {
4658 case MEM_TYPE_NON_LISP: 4673 case MEM_TYPE_NON_LISP:
4659 return 0; 4674 return 0;