Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ allocb(9F) — SunOS 5.1

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

bufcall(9F)

esballoc(9F)

esbbcall(9F)

testb(9F)

copyb(9F)

msgb(9S)

dupb(9F)

NAME

allocb − allocate a message block

SYNOPSIS

#include <sys/stream.h>
mblk_t ∗allocb(int size, uint pri);

ARGUMENTS

size The number of bytes in the message block. 

pri Priority of the request (no longer used). 

INTERFACE LEVEL

Architecture independent level 1 (DDI/DKI). 

DESCRIPTION

allocb() tries to allocate a STREAMS message block.  Buffer allocation fails only when the system is out of memory. If no buffer is available, the bufcall(9F) function can help a module recover from an allocation failure. 

The following figure identifies the data structure members that are affected when a message block is allocated. 













b_cont (0)
b_rptr
b_wptr
b_datap
message block
(mblk_t)
data block
(dblk_t)
data buffer
















.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.




db_base
db_lim
db_type (M_DATA)

RETURN VALUES

allocb() returns a pointer to the allocated message block of type M_DATA (defined in sys/stream.h) on success.  On failure, it returns a NULL pointer. 

CONTEXT

allocb() can be called from user or interrupt context.

EXAMPLE

Given a pointer to a queue ( q ) and an error number ( err ), the send_error() routine sends an M_ERROR type message to the stream head. 

If a message cannot be allocated, NULL is returned, indicating an allocation failure (line 8).  Otherwise, the message type is set to M_ERROR (line 10).  Line 11 increments the write pointer (bp->b_wptr) by the size (one byte) of the data in the message. 

A message must be sent up the read side of the stream to arrive at the stream head.  To determine whether q points to a read queue or to a write queue, the q->q_flag member is tested to see if QREADR is set (line 13).  If it is not set, q points to a write queue, and in line 14 the RD(9F) function is used to find the corresponding read queue.  In line 15, the putnext(9F) function is used to send the message upstream, returning 1 if successful. 

 1  send_error(q,err)
 2         queue_t ∗q;
 3         unsigned char err;
 4  {
 5         mblk_t ∗bp;
 6
 7         if ((bp = allocb(1, BPRI_HI)) == NULL) /∗ allocate msg. block ∗/
 8              return(0);
 9
10         bp->b_datap->db_type = M_ERROR;   /∗ set msg type to M_ERROR ∗/
11         ∗bp->b_wptr++ = err;              /∗ increment write pointer ∗/
12
13         if(!q->q_flag & QREADR))          /∗ if not read queue     ∗/
14              q = RD(q);                   /∗    get read queue     ∗/
15         putnext(q,bp);                    /∗ send message upstream ∗/
16         return(1);
17  }

SEE ALSO

bufcall(9F), esballoc(9F), esbbcall(9F), testb(9F)

SunOS 5.1 Writing Device Drivers
SunOS 5.1 STREAMS Programmer’s Guide

NOTES

The pri argument is no longer used, but is retained for compatibility with existing drivers.  ---------- X-Sun-Data-Type: default X-Sun-Data-Name: dupb.9f X-Sun-Content-Lines: 539
 

NAME

dupb − duplicate a message block descriptor

SYNOPSIS

#include <sys/stream.h>
mblk_t ∗dupb(mblk_t ∗bp);

ARGUMENTS

bp Pointer to the message block to be duplicated.  mblk_t is an instance of the msgb(9S) structure. 

INTERFACE LEVEL

Architecture independent level 1 (DDI/DKI). 

DESCRIPTION

dupb() creates a new mblk_t structure to reference the message block pointed to by bp. Unlike copyb(9F), dupb does not copy the information in the data block, but creates a new structure to point to it. 

The following figure shows how the db_ref field of the dblk_t structure has been changed from 1 to 2, reflecting the increase in the number of references to the data block.  The new mblk_t contains the same information as the first.  Note that b_rptr and b_wptr are copied from bp, and that db_ref is incremented. 


















.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.




.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.











nbp=dupb(bp);



Before
After
db_base
db_ref (2)








db_base
db_ref (1)
bp






b_datap


b_rptr
b_wptr




b_datap


b_rptr
b_wptr




b_datap


b_rptr
b_wptr




nbp






















bp

RETURN VALUES

If successful, dupb returns a pointer to the new message block.  Otherwise, it returns a NULL pointer. 

CONTEXT

dupb() can be called from user or interrupt context. 

EXAMPLE

This srv(9E) (service) routine adds a header to all M_DATA messages before passing them along.  The message block for the header was allocated elsewhere.  For each message on the queue, if it is a priority message, pass it along immediately (lines 9−10).  Otherwise, if it is anything other than an M_DATA message (line 11), and if it can be sent along (line 12), then do so (line 13).  Otherwise, put the message back on the queue and return (lines 15−16).  For all M_DATA messages, first check to see if the stream is flow-controlled (line 19).  If it is, put the message back on the queue and return (line 22); if it is not, the header block is duplicated (line 20).  If dupb fails, the service routine is rescheduled in one tenth of a second ( (HZ/10) with timeout and then we return (lines 23−24).  If dupb succeeds, link the M_DATA message to it (line 26) and pass it along (line 27).  dupb can be used here instead of copyb(9F) because the contents of the header block are not changed. 

Note that this example ignores issues related to cancelling outstanding timeouts at close time. 

 1  xxxsrv(q)
 2      queue_t ∗q;
 3  {
 4mblk_t ∗mp;
 5 mblk_t ∗bp;
 6extern mblk_t ∗hdr;
 7
 8while ((mp = getq(q)) != NULL) {
 9if (mp->b_datap->db_type >= QPCTL) {
10putnext(q, mp);
11} else if (mp->b_datap->db_type != M_DATA) {
12if (canput(q->q_next))
13putnext(q, mp);
14else {
15putbq(q, mp);
16return;
17}
18} else {/∗ M_DATA ∗/
19if (canput(q->q_next)) {
20bp = dupb(hdr);
21if (bp == NULL) {
22putbq(q, mp);
23timeout(qenable, (long)q, HZ/10);
24return;
25}
26linkb(bp, mp);
27putnext(q, bp);
28} else {
29putbq(q, mp);
30return;
31}
32}
33}
34  }

SEE ALSO

copyb(9F), msgb(9S)

SunOS 5.1 STREAMS Programmer’s Guide

SunOS 5.1  —  Last change: 11 Apr 1991  —  Last change: 11 Apr 1991

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026