Skip to content

Commit dfe0711

Browse files
committed
GEOM: add a new function g_new_geom
This function is a variant of g_new_geomf. It accepts a regular string instead of a format string as its input parameter. It can save the time wasted on unnecessary format string processing.
1 parent bfa4759 commit dfe0711

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

share/man/man9/g_geom.9

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@
2727
.Os
2828
.Sh NAME
2929
.Nm g_new_geomf ,
30+
.Nm g_new_geom ,
3031
.Nm g_destroy_geom
3132
.Nd "geom management"
3233
.Sh SYNOPSIS
3334
.In geom/geom.h
3435
.Ft "struct g_geom *"
3536
.Fn g_new_geomf "struct g_class *mp" "const char *fmt" ...
37+
.Ft "struct g_geom *"
38+
.Fn g_new_geom "struct g_class *mp" "const char *name"
3639
.Ft void
3740
.Fn g_destroy_geom "struct g_geom *gp"
3841
.Sh DESCRIPTION
@@ -58,6 +61,14 @@ The geom's name is created in a
5861
-like way from the rest of the arguments.
5962
.Pp
6063
The
64+
.Fn g_new_geom
65+
function is very similar to
66+
.Fn g_new_geomf
67+
except that it accepts a regular string instead of a
68+
.Xr printf 3 Ns
69+
-like format strng as the geom's name.
70+
.Pp
71+
The
6172
.Fn g_destroy_geom
6273
function destroys the given geom immediately and cancels all related pending
6374
events.
@@ -94,7 +105,9 @@ and
94105
.Va access
95106
for it.
96107
.Pp
97-
.Fn g_new_geomf :
108+
.Fn g_new_geomf
109+
and
110+
.Fn g_new_geom :
98111
.Bl -item -offset indent
99112
.It
100113
Class

sys/geom/geom.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,9 @@ int g_handleattr_int(struct bio *bp, const char *attribute, int val);
289289
int g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val);
290290
int g_handleattr_uint16_t(struct bio *bp, const char *attribute, uint16_t val);
291291
int g_handleattr_str(struct bio *bp, const char *attribute, const char *str);
292-
struct g_consumer * g_new_consumer(struct g_geom *gp);
293-
struct g_geom * g_new_geomf(struct g_class *mp, const char *fmt, ...)
292+
struct g_consumer *g_new_consumer(struct g_geom *gp);
293+
struct g_geom *g_new_geom(struct g_class *mp, const char *name);
294+
struct g_geom *g_new_geomf(struct g_class *mp, const char *fmt, ...)
294295
__printflike(2, 3);
295296
struct g_provider * g_new_providerf(struct g_geom *gp, const char *fmt, ...)
296297
__printflike(2, 3);

sys/geom/geom_subr.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -368,29 +368,23 @@ g_retaste(struct g_class *mp)
368368
}
369369

370370
struct g_geom *
371-
g_new_geomf(struct g_class *mp, const char *fmt, ...)
371+
g_new_geom(struct g_class *mp, const char *name)
372372
{
373+
int len;
373374
struct g_geom *gp;
374-
va_list ap;
375-
struct sbuf *sb;
376375

377376
g_topology_assert();
378377
G_VALID_CLASS(mp);
379-
sb = sbuf_new_auto();
380-
va_start(ap, fmt);
381-
sbuf_vprintf(sb, fmt, ap);
382-
va_end(ap);
383-
sbuf_finish(sb);
384-
gp = g_malloc(sizeof *gp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
378+
len = strlen(name);
379+
gp = g_malloc(sizeof *gp + len + 1, M_WAITOK | M_ZERO);
385380
gp->name = (char *)(gp + 1);
386381
gp->class = mp;
387382
gp->rank = 1;
388383
LIST_INIT(&gp->consumer);
389384
LIST_INIT(&gp->provider);
390385
LIST_INSERT_HEAD(&mp->geom, gp, geom);
391386
TAILQ_INSERT_HEAD(&geoms, gp, geoms);
392-
strcpy(gp->name, sbuf_data(sb));
393-
sbuf_delete(sb);
387+
memcpy(gp->name, name, len);
394388
/* Fill in defaults from class */
395389
gp->start = mp->start;
396390
gp->spoiled = mp->spoiled;
@@ -404,6 +398,23 @@ g_new_geomf(struct g_class *mp, const char *fmt, ...)
404398
return (gp);
405399
}
406400

401+
struct g_geom *
402+
g_new_geomf(struct g_class *mp, const char *fmt, ...)
403+
{
404+
struct g_geom *gp;
405+
va_list ap;
406+
struct sbuf *sb;
407+
408+
sb = sbuf_new_auto();
409+
va_start(ap, fmt);
410+
sbuf_vprintf(sb, fmt, ap);
411+
va_end(ap);
412+
sbuf_finish(sb);
413+
gp = g_new_geom(mp, sbuf_data(sb));
414+
sbuf_delete(sb);
415+
return (gp);
416+
}
417+
407418
void
408419
g_destroy_geom(struct g_geom *gp)
409420
{

0 commit comments

Comments
 (0)