mpool.libtp

来自「基于组件方式开发操作系统的OSKIT源代码」· LIBTP 代码 · 共 747 行 · 第 1/2 页

LIBTP
747 行
字号
	}	if (RELEASE_MASTER){		/* Memory leak */		return(NULL);	}	if ( bhp ) {	    return ((ADDR_T)(buf_table+(bhp-bufhdr_table)));	} else {	    return ( NULL );	}}/*	MIS - do I want to add file links to buffer pool?*/extern intbuf_sync ( fid, close )int	fid;int	close;		/* should we dec refcount and possibly			   invalidate all the buffers */{    int	i;    int	fd;    int	invalidate;    BUFHDR_T	*bhp;    if ( (fd = bf_fid_to_fd ( fid )) < 0 ) {	return(1);    }    if (GET_MASTER) {	return(1);    }    invalidate = (buf_fids[fid].refcount == 1 && close);    if ( invalidate )	for ( bhp = bufhdr_table, i = 0; i < NUM_BUFS; bhp++, i++ ) {	    if (bhp->id.file_id == fid) {		if ((bhp->flags & BF_DIRTY) && (bf_put_page( fd, bhp ) < 0)) {			return(1);		}		bhp->id.file_id = -1;	    }	}    if (invalidate || close)	buf_fids[fid].refcount--;    if (RELEASE_MASTER) {	return(1);    }    return(0);}extern intbuf_flags ( addr, set_flags, unset_flags )ADDR_T	addr;u_long	set_flags;u_long	unset_flags;{    int		bufid;    BUFHDR_T	*bhp;#ifdef PIN_DEBUG	fprintf(stderr, "buf_flags: %X setting %s%s%s%s%s releasing %s%s%s%s%s\n",	    addr, 	    set_flags&BUF_DIRTY ? "DIRTY " : "", 	    set_flags&BUF_VALID ? "VALID " : "", 	    set_flags&BUF_PINNED ? "PINNED " : "", 	    set_flags&BUF_IO_ERROR ? "IO_ERROR " : "", 	    set_flags&BUF_IO_IN_PROGRESS ? "IO_IN_PROG " : "", 	    set_flags&BUF_NEWPAGE ? "NEWPAGE " : "", 	    unset_flags&BUF_DIRTY ? "DIRTY " : "", 	    unset_flags&BUF_VALID ? "VALID " : "", 	    unset_flags&BUF_PINNED ? "PINNED " : "", 	    unset_flags&BUF_IO_ERROR ? "IO_ERROR " : "", 	    unset_flags&BUF_IO_IN_PROGRESS ? "IO_IN_PROG " : "", 	    unset_flags&BUF_NEWPAGE ? "NEWPAGE " : "" );#endif    if (!ADDR_OK(addr)) {	error_log1 ( "buf_pin: Invalid Buffer Address %x\n", addr );	return(1);    }    bufid = ((BUF_T *)addr) - buf_table;    assert ( bufid < NUM_BUFS);    bhp = &bufhdr_table[bufid];    if (GET_MASTER) {	return(1);    }    bhp->flags |= set_flags;    if ( set_flags & BUF_PINNED ) {	bhp->refcount++;    }    if ( set_flags & BUF_DIRTY ) {	unset_flags |= BUF_NEWPAGE;    }    if ( unset_flags & BUF_PINNED ) {	bhp->refcount--;	if ( bhp->refcount ) {		/* Turn off pin bit so it doesn't get unset */		unset_flags &= ~BUF_PINNED;	}    }    bhp->flags &= ~unset_flags;    MAKE_MRU(bhp);    if (RELEASE_MASTER) {	return(1);    }    return(0);}/*	Take a string name and produce an fid.	returns -1 on error	MIS -- this is a potential problem -- you keep actual names		here -- what if people run from different directories?*/extern	intbuf_name_lookup ( fname )char	*fname;{    int	i;    int	fid;    int	ndx;    fid = -1;    if (GET_MASTER) {	    return(-1);    }    for ( i = 0; i < NUM_FILE_ENTRIES; i++ ) {	if ( buf_fids[i].offset == -1 ) {		fid = i;	} else {		if (!strcmp (fname, buf_strings+buf_fids[i].offset)) {			if (RELEASE_MASTER) {				return(-1);			}			buf_fids[i].refcount++;			return(i);		}	}    }    if ( fid == -1 ) {	error_log0 ( "No more file ID's\n" );    } else {	ndx = *buf_sp - strlen(fname) - 1;	if ( ndx < 0 ) {	    error_log0 ( "Out of string space\n" );	    fid = -1;	} else {	    *buf_sp = ndx;	    strcpy ( buf_strings+ndx, fname );	    buf_fids[fid].offset = ndx;	}	buf_fids[fid].refcount = 1;    }    if (RELEASE_MASTER) {	return(-1);    }    return(fid);}static	intbf_fid_to_fd ( fid ) int	fid;{	struct stat sbuf;	assert ( (fid < NUM_FILE_ENTRIES) && (buf_fids[fid].offset != -1) );	if ( fds[fid] != -1 ) {	    return(fds[fid]);	}	fds[fid] = open ( buf_strings+buf_fids[fid].offset, O_RDWR|O_CREAT, 			  0666 );	if ( fds[fid] < 0 ) {	    error_log3 ( "Error Opening File %s FID: %d FD: %d.  Errno = %d\n", 			    buf_strings+buf_fids[fid].offset, fid, fds[fid], 			    errno );	    return(-1);	}	error_log3 ( "Opening File %s FID: %d FD: %d\n", 			buf_strings+buf_fids[fid].offset, fid, fds[fid] );	if ( buf_fids[fid].npages == -1 ) {		/* Initialize the npages field */		if ( fstat ( fds[fid], &sbuf ) ) {		    error_log3 ( "Error Fstating %s FID: %d.  Errno = %d\n", 				buf_strings+buf_fids[fid].offset, fid, errno );		} else {		    buf_fids[fid].npages = ( sbuf.st_size / BUFSIZE );		}	}	return ( fds[fid] );}static intbf_put_page ( fd, bhp ) int	fd;BUFHDR_T	*bhp;{	int	nbytes;	assert ( (bhp-bufhdr_table) < NUM_BUFS );	if ( lseek ( fd, bhp->id.obj_id << BUFSHIFT, L_SET ) < 0 ) {	    return(-1);	}	bhp->flags |= BUF_IO_IN_PROGRESS;	if (RELEASE_MASTER) {	    return(-1);	}	nbytes = write(fd, buf_table[bhp-bufhdr_table], BUFSIZE);	if (GET_MASTER) {	    return(-2);	}	if ( nbytes < 0 ) {		error_log1 ("Write failed with error code %d\n", errno);		return(-1);	} else if ( nbytes != BUFSIZE ) {		error_log1 ("Short write: %d bytes of %d\n", nbytes, BUFSIZE );	}	bhp->flags &= ~(BUF_DIRTY|BUF_IO_IN_PROGRESS);	return (0);}static BUFHDR_T	*bf_assign_buf ( ndx, obj, flags, len )int	ndx;OBJ_T	*obj;u_long	flags;int	*len;		/* Number of bytes read */{    BUFHDR_T	*bhp;    int		fd;    assert ( obj->file_id < NUM_FILE_ENTRIES );    bhp = bf_newbuf();    if ( !bhp ) {	return(NULL);    }    OBJ_ASSIGN ( (*obj), bhp->id );    if ( buf_hash_table[ndx] >= NUM_BUFS ) {	buf_hash_table[ndx] = bhp-bufhdr_table;    } else {	LISTPE_INSERT ( bufhdr_table, hash, bhp, buf_hash_table[ndx] );    }    bhp->flags |= BUF_VALID;    if ( flags & BF_PIN ) {	bhp->flags |= BUF_PINNED;	bhp->refcount++;#ifdef PIN_DEBUG	fprintf(stderr, "bf_assign_buf: %X PINNED (%d)\n", 		buf_table + (bhp-bufhdr_table), bhp->refcount);#endif    }    fd = bf_fid_to_fd(obj->file_id);    if ( fd == -1 ) {	error_log1 ("Invalid fid %d\n", obj->file_id);	bhp->flags |= ~BUF_IO_ERROR;	return(NULL);    }    if ( obj->obj_id >= buf_fids[obj->file_id].npages) {	buf_fids[obj->file_id].npages = obj->obj_id+1;	*len = 0;    } else if ( flags & BF_EMPTY ) {	*len = 0;    } else {	bhp->flags |= BUF_IO_IN_PROGRESS;	if (RELEASE_MASTER) {		return(NULL);	}	if ( lseek ( fd, obj->obj_id << BUFSHIFT, L_SET ) < -1 ) {	    error_log2 ("Unable to perform seek on file: %d  to page %d",			obj->file_id, obj->obj_id );	    bhp->flags &= ~BUF_IO_IN_PROGRESS;	    bhp->flags |= ~BUF_IO_ERROR;	    return(NULL);	}	*len = read(fd, buf_table[bhp-bufhdr_table], BUFSIZE);	if ( *len < 0 ) {	    error_log2 ("Unable to perform read on file: %d  to page %d",			obj->file_id, obj->obj_id );	    bhp->flags &= ~BUF_IO_IN_PROGRESS;	    bhp->flags |= ~BUF_IO_ERROR;	    return(NULL);	} 	if (GET_MASTER) {		return(NULL);	}	bhp->flags &= ~BUF_IO_IN_PROGRESS;	if ( bhp->wait_proc != -1 ) {	    /* wake up waiter and anyone waiting on it */#ifdef DEBUG	    printf("Waking transaction %d due to completed I/O\n", 		bhp->wait_proc);#endif	    proc_wake_id ( bhp->wait_proc );	    bhp->wait_proc = -1;	}	MAKE_MRU(bhp);    }    if ( flags & BF_DIRTY ) {	bhp->flags |= BUF_DIRTY;    } else if ( *len < BUFSIZE ) {	bhp->flags |= BUF_NEWPAGE;    }    return ( bhp );}intbuf_last ( fid )int	fid;{	int	val;	if (GET_MASTER) {		return(-1);	}	assert ( fid < NUM_FILE_ENTRIES );	if ( buf_fids[fid].npages == -1 ) {	    /* initialize npages field */	    (void) bf_fid_to_fd ( fid );	}	val = buf_fids[fid].npages;		if ( val ) {		val--;			/* Convert to page number */	}	if (RELEASE_MASTER) {		return(-1);	}	return(val);}#ifdef DEBUGextern voidbuf_dump ( id, all )int	id;int	all;{	int i;	BUFHDR_T	*bhp;	printf ( "LRU + %d\n", *buf_lru );	if ( all ) {	    printf("ID\tFID\tPID\tLNEXT\tLPREV\tHNEXT\tHPREV\tSLEEP\tFLAG\tREFS\n");	    for ( bhp = bufhdr_table, i = 0; i < NUM_BUFS; bhp++, i++ ) {		    printf ( "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%x\t%d\n", i,			bhp->id.file_id, bhp->id.obj_id,			bhp->lru.next, bhp->lru.prev,			bhp->hash.next, bhp->hash.prev,			bhp->wait_proc, bhp->flags, bhp->refcount );	    }	} else {		if ( id >= NUM_BUFS ) {			printf ( "Buffer ID (%d) too high\n", id );			return;		}		bhp = bufhdr_table+id;		printf ( "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%x\t%d\n", i,		    bhp->id.file_id, bhp->id.obj_id,		    bhp->lru.next, bhp->lru.prev,		    bhp->hash.next, bhp->hash.prev,		    bhp->wait_proc, bhp->flags, bhp->refcount );	}	return;}#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?